STM32簡易交通燈設計(定時可調)
//-----定時設置函數-------
void LED_SET_GREEN(u8 k)
{
LCD_ShowNum(160,235,k,2,16);
}
void LED_SET_YELLOW(u8 k)
{
LCD_ShowNum(160,145,k,2,16);
}
void LED_SET_RED(u8 k)
{
LCD_ShowNum(160,55,k,2,16);
}
//-----定時器中斷函數---掃描按鍵和顯示設定值用----
void TIM3_IRQHandler(void)
{
if(TIM3->SR&0x0001)
{
scan_key1();
scan_key2();
scan_key3();
LED_SET_RED(R);
LED_SET_GREEN(G);
LED_SET_YELLOW(Y);
}
TIM3->SR=~(1<<0);
}
//------主函數-----------
int main(void)
{
u16 code; //TFT驅動芯片代號
int i;
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,9600);
printf("uart is ok. ");
Timer_init(300,7199); //定時器初始化 設定溢出時間是 30ms
printf("time_init is ok. ");
LCD_Init(); //TFT-LCD初始化
printf("lcd_init is ok.");
key_init(); //按鍵I/O口設置初始化
printf("key_init is ok.");
JTAG_Set(JTAG_SWD_DISABLE); //關閉JTAG調試器
code=LCD_ReadReg(0x0000);
delay_ms(500);
printf("deiver code is %ld",code); //讀取驅動芯片型號是發(fā)送到串口
LCD_Fill(30,200,100,270,BLACK);
LCD_Fill(30,110,100,180,BLACK);
LCD_Fill(30,20,100,90,BLACK);
LCD_ShowString(110,15,"Show");
LCD_ShowString(110,30,"Time");
LCD_ShowString(160,15,"Set ");
LCD_ShowString(160,30,"Time");
LED_GREEN(0);
LED_YELLOW(0);
LED_RED(0);
while(1)
{
for(i=G;i>=0;i--) //綠燈控制
{
LED_GREEN(i);
LCD_Fill(30,200,100,270,GREEN);
delay_ms(1000);
LCD_Fill(30,200,100,270,BLACK);
delay_ms(1000);
}
for(i=Y;i>=0;i--) //黃燈控制
{
LED_YELLOW(i);
LCD_Fill(30,110,100,180,YELLOW);
delay_ms(1000);
LCD_Fill(30,110,100,180,BLACK);
delay_ms(1000);
}
for(i=R;i>=0;i--) //紅燈控制
{
LED_RED(i);
LCD_Fill(30,20,100,90,RED);
delay_ms(1000);
LCD_Fill(30,20,100,90,BLACK);
delay_ms(1000);
}
}
}
//-------各類庫函數省略---------
//-------各類庫函數省略---------
評論