SWM166-SPI-Y1.28C1圆形屏,采用SWM166作为主控,这个单片机第一次用。先搞了GPIO测试:
参照原理图。LED与A1,A2,A3链接。这个LED 电路驱动很巧妙。
主函数:
延时函数,时钟初始化,GPIO配置,这是基础功。
#include "SWM166.h"
void delay_us(unsigned int time)
{
unsigned int i=0;
while(time--)
{
i=10; //????
while(i--) ;
}
}
void delay_ms(unsigned int time)
{
unsigned int i=0;
while(time--)
{
i=12000; //????
while(i--) ;
}
}
int main(void)
{
SystemInit();
GPIO_Init(GPIOA, PIN2, 0, 1, 0, 0); //ÊäÈ룬ÉÏÀʹÄÜ£¬½ÓKEY
GPIO_Init(GPIOA, PIN1, 1, 0, 0, 0); //Êä³ö£¬ ½ÓLED
GPIO_Init(GPIOA, PIN2, 1, 0, 0, 0); //Êä³ö£¬ ½ÓLED
GPIO_Init(GPIOA, PIN3, 1, 0, 0, 0); //Êä³ö£¬ ½ÓLED
while(1==1)
{
GPIO_SetBit(GPIOA, PIN1);
GPIO_SetBit(GPIOA, PIN2);
GPIO_SetBit(GPIOA, PIN3);
delay_ms(1000);
GPIO_ClrBit(GPIOA, PIN1);
GPIO_ClrBit(GPIOA, PIN2);
GPIO_ClrBit(GPIOA, PIN3);
delay_ms(1000);
}
}
void GPIO_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down, uint32_t open_drain)
{
PORT_TypeDef * PORTx = PORTA;
switch((uint32_t)GPIOx)
{
case ((uint32_t)GPIOA):
SYS->CLKEN0 |= (0x01 << SYS_CLKEN0_GPIOA_Pos);
PORTx = PORTA;
break;
case ((uint32_t)GPIOB):
SYS->CLKEN0 |= (0x01 << SYS_CLKEN0_GPIOB_Pos);
PORTx = PORTB;
break;
case ((uint32_t)GPIOM):
SYS->CLKEN0 |= (0x01 << SYS_CLKEN0_GPIOM_Pos);
PORTx = PORTM;
break;
}
PORT_Init(PORTx, n, 0, 1); //PORTx.PINnÒý½ÅÅäÖÃΪGPIO¹¦ÄÜ£¬Êý×ÖÊäÈ뿪Æô
if(dir == 1)
{
GPIOx->DIR |= (0x01 << n);
}
else
{
GPIOx->DIR &= ~(0x01 << n);
}
if(pull_up == 1) PORTx->PULLU |= (1 << n);
else PORTx->PULLU &= ~(1 << n);
if(pull_down == 1) PORTx->PULLD |= (1 << n);
else PORTx->PULLD &= ~(1 << n);
if(open_drain == 1) PORTx->OPEND |= (1 << n);
else PORTx->OPEND &= ~(1 << n);
}
/******************************************************************************************************************************************
**时钟初始化,此函数用于更新变量SystemCoreClock,每当核心时钟发生变化时必须调用此函数
******************************************************************************************************************************************/
void SystemInit(void)
{
SYS->CLKEN0 |= (1 << SYS_CLKEN0_ANAC_Pos);
if((SYS->BACKUP[3] >> 29) == 5)
*((__IO uint32_t *)((uint32_t)&SYS->HRCCR + 8)) = SYS->BACKUP[3] & (~(7u << 29));
Flash_Param_at_xMHz(90);
switch(SYS_CLK)
{
case SYS_CLK_12MHz:
switchTo12MHz();
break;
case SYS_CLK_1M5Hz:
switchTo1M5Hz();
break;
case SYS_CLK_XTAL:
switchToXTAL(0);
break;
case SYS_CLK_XTAL_DIV8:
switchToXTAL(1);
break;
case SYS_CLK_PLL:
switchToPLL(0);
break;
case SYS_CLK_PLL_DIV8:
switchToPLL(1);
break;
case SYS_CLK_32KHz:
switchTo32KHz();
break;
}
SystemCoreClockUpdate();
Flash_Param_at_xMHz(SystemCoreClock/1000000);
FMC->CACHE = (1 << FMC_CACHE_CEN_Pos) | (1 << FMC_CACHE_CCLR_Pos); // Çå³ý Cache
__NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
FMC->CACHE = (1 << FMC_CACHE_CEN_Pos) | (1 << FMC_CACHE_CPEN_Pos);
PORTB->PULLD &= ~((1 << PIN10) | (1 << PIN11));
PORTB->PULLU &= ~((1 << PIN12) | (1 << PIN14));
SYS->PGACR &= ~SYS_PGACR_VRTRIM_Msk; //PGAÕýÊäÈë¶Ë²Î¿¼µçѹУ׼
SYS->PGACR |= ((SYS->CHIPID[3] & 0x000F) << SYS_PGACR_VRTRIM_Pos);
SYS->PGACR &= ~SYS_PGACR_DATRIM_Msk; //ÓÃÓÚ²úÉúACMP NÊäÈë¶ËµçѹµÄDACµÄ²Î¿¼µçѹУ׼
SYS->PGACR |= (((SYS->CHIPID[3] >> 16) & 0x000F) << SYS_PGACR_DATRIM_Pos);
}
|