近期在备战蓝桥杯嵌入式入队比赛,这是做的第一个编程题目,我会将自己做了的题目都记录下来,但是现在就没时间去详细的讲解了,只能先把代码记录下来,然后比赛完后在进行分析。
main.c
#include "led.h"
#include "usart.h"
#include "lcd.h"
#include "key.h"
#include "pwm.h"
#include "eeprom.h"
#include "rtc.h"
#include "string.h"
static __INLINE uint32_t SysTick_Config1(uint32_t ticks);
void window_show(void);
extern u8 receive,uart_index;
extern u8 Rxbuffer[25];
extern u32 Time;
extern u8 key1_sta,key3_sta;
extern int CH2_val,CH3_val;
u8 cmd_stat=0,port=0;
int time_start=0,time_stop=0;
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SysTick_Config1(SystemCoreClock/1000);
led_init();
key_init();
rtc_init();
at24c02_init();
usart2_init();
TIM2_pwm_init(1000-1,72-1);
STM3210B_LCD_Init();
delay_ms(500);
LCD_Clear(White);
LCD_SetTextColor(Blue);
LCD_SetBackColor(White);
while(1)
{
window_show();
delay_ms(100);
}
}
static __INLINE uint32_t SysTick_Config1(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, 1); /* set Priority for Cortex-M0 System Interrupts */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0); /* Function successful */
}
void window_show(void)
{
u8 str1[20];
u8 len;
int hh,mm,ss,hold_sec;
int ch2_val,ch3_val;
ch2_val=at24c02_read_int(0x01);
delay_ms(2);
ch3_val=at24c02_read_int(0x10);
delay_ms(2);
TIM_SetCompare2(TIM2,ch2_val*10);
TIM_SetCompare3(TIM2,ch3_val*10);
sprintf(str1,"PWM-PA1:%d%% ",ch2_val);
LCD_DisplayStringLine(Line1, str1);
sprintf(str1,"PWM-PA2:%d%% ",ch3_val);
LCD_DisplayStringLine(Line2, str1);
Time = RTC_GetCounter();
sprintf(str1,"Time: %0.2d:%0.2d:%0.2d",Time/3600,(Time%3600)/60,(Time%3600)%60);
LCD_DisplayStringLine(Line3, str1);
if(key1_sta==1)
{
//熄灭LED1
led_ctl(LED1,0);
//点亮LED0
led_ctl(LED0,1);
LCD_DisplayStringLine(Line4, "Channel:PA1");
}
else if(key3_sta==1)
{
//熄灭LED0
led_ctl(LED0,0);
//点亮LED1
led_ctl(LED1,1);
LCD_DisplayStringLine(Line4, "Channel:PA2");
}
else
{
key1_sta=0;
key3_sta=0;
//熄灭LED0
led_ctl(LED0,0);
//熄灭LED1
led_ctl(LED1,0);
LCD_DisplayStringLine(Line4, "Channel: ");
}
if(receive==1)
{
len=strlen(Rxbuffer);
LCD_DisplayStringLine(Line6,Rxbuffer);
hh=(Rxbuffer[0]-48)*10+(Rxbuffer[1]-48);
mm=(Rxbuffer[3]-48)*10+(Rxbuffer[4]-48);
ss=(Rxbuffer[6]-48)*10+(Rxbuffer[7]-48);
hold_sec=Rxbuffer[13]-48;
port=Rxbuffer[11]-48;
time_start=hh*3600+mm*60+ss;
time_stop=time_start+hold_sec;
cmd_stat=1;
}
else
{
LCD_DisplayStringLine(Line6," ");
}
LCD_DisplayStringLine(Line5, "Command:");
}
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
#define LED_ALL GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15
#define LED0 GPIO_Pin_8
#define LED1 GPIO_Pin_9
#define LED2 GPIO_Pin_10
#define LED3 GPIO_Pin_11
#define LED4 GPIO_Pin_12
#define LED5 GPIO_Pin_13
#define LED6 GPIO_Pin_14
#define LED7 GPIO_Pin_15
void led_init(void);
void led_ctl(u16 led,u8 status);
#endif
led.c
#include "led.h"
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_ALL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,LED_ALL);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
void led_ctl(u16 led,u8 status)
{
if(status==1)
{
GPIO_SetBits(GPIOC,LED_ALL);
GPIO_ResetBits(GPIOC,led);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
else if(status==0)
{
GPIO_SetBits(GPIOC,LED_ALL);
GPIO_SetBits(GPIOC,led);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
}
usart.h
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
void usart2_init(void);
void usart2_senddata(u8 *str);
#endif
usart.c
#include "usart.h"
#include "string.h"
u8 receive=0,uart_index=0;
u8 Rxbuffer[25];
void usart2_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Mode = USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void usart2_senddata(u8 *str)
{
int i=0;
do{
USART_SendData(USART2,str[i]);
while(!USART_GetFlagStatus(USART2,USART_FLAG_TXE));
i++;
}while(str[i]!=0);
}
void USART2_IRQHandler(void)
{
u8 temp;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
temp=USART_ReceiveData(USART2);
if((temp=='S')||(uart_index==20))
{
Rxbuffer[uart_index++]=temp;
if(strlen(Rxbuffer)==15)
{
receive=1;
uart_index=0;
}
else
uart_index=0;
}
else
{
Rxbuffer[uart_index++]=temp;
}
}
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "stm32f10x.h"
void key_init(void);
#endif
key.c
#include "key.h"
#include "usart.h"
#include "led.h"
#include "eeprom.h"
u8 key1_sta=0,key3_sta=0;
int CH2_val=100,CH3_val=100;
void key_init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
/* Configure EXTI8 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
/* Configure EXTI8 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2);
/* Configure EXTI8 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
//按键1中断处理函数
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
delay_ms(10);
//led_ctl(LED0,1);
if(key1_sta==0)
{
//打开该通道
TIM2->CCER|=1<<4;
TIM_SetCompare2(TIM2,CH2_val);
//关闭另外一个通道
key3_sta=0;
TIM2->CCER&=~(1<<8);
//改变状态标志
key1_sta=1;
}
else if(key1_sta==1)
{
//关闭该通道
TIM2->CCER&=~(1<<4);
key1_sta=0;
}
//delay_ms(10);
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
//按键2中断处理函数
void EXTI9_5_IRQHandler(void)
{
u8 *str;
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
delay_ms(10);
if(key1_sta==1)
{
if((CH2_val!=0)&&(CH2_val<900))
{
CH2_val+=100;
}
else
{
CH2_val=100;
}
at24c02_write_int(0x01,(int)(CH2_val/10));
}
//存储到EEPROM
EXTI_ClearITPendingBit(EXTI_Line8);
}
}
//按键3中断处理函数
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
delay_ms(10);
if(key3_sta==0)
{
//打开该通道
TIM2->CCER|=1<<8;
TIM_SetCompare3(TIM2,CH3_val);
//关闭另外一个通道
key1_sta=0;
TIM2->CCER&=~(1<<4);
//改变状态标志
key3_sta=1;
}
else if(key3_sta==1)
{
//关闭该通道
TIM2->CCER&=~(1<<8);
key3_sta=0;
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
//按键4中断处理函数
void EXTI2_IRQHandler(void)
{
u8 *str;
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
delay_ms(10);
if(key3_sta==1)
{
if((CH3_val!=0)&&(CH3_val<900))
{
CH3_val+=100;
}
else
{
CH3_val=100;
}
at24c02_write_int(0x10,(int)(CH3_val/10));
}
//存储到EEPROM
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
eeprom.h
#ifndef __EEPROM_H
#define __EEPROM_H
#include "stm32f10x.h"
void at24c02_init(void);
u8 at24c02_read(u8 addr);
void at24c02_write(u8 addr,u8 data);
int at24c02_read_int(u8 addr);
void at24c02_write_int(u8 addr,int data);
#endif
eeprom.c
#include "eeprom.h"
#include "i2c.h"
void at24c02_init(void)
{
i2c_init();
}
u8 at24c02_read(u8 addr)
{
u8 data;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
data=I2CReceiveByte();
I2CSendAck();
return data;
}
void at24c02_write(u8 addr,u8 data)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
delay_ms(10);
}
int at24c02_read_int(u8 addr)
{
int data;
data=(int)at24c02_read(addr);
delay_ms(3);
data+=(int)at24c02_read(addr+1)<<8;
delay_ms(3);
data+=(int)at24c02_read(addr+2)<<16;
delay_ms(3);
data+=(int)at24c02_read(addr+3)<<24;
delay_ms(3);
return data;
}
void at24c02_write_int(u8 addr,int data)
{
at24c02_write(addr,(data&0xFF));
delay_ms(1);
at24c02_write(addr+1,(data>>8&0xFF));
delay_ms(1);
at24c02_write(addr+2,(data>>16&0xFF));
delay_ms(1);
at24c02_write(addr+3,(data>>24&0xFF));
delay_ms(1);
}
pwm.h
#ifndef __PWM_H
#define __PWM_H
#include"stm32f10x.h"
void TIM2_pwm_init(u16 arr,u16 psc);
#endif
pwm.c
#include "pwm.h"
void TIM2_pwm_init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//OC2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
//OC3
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM2, ENABLE);
TIM2->CCER&=~(1<<4);
TIM2->CCER&=~(1<<8);
}
rtc.h
#ifndef __RTC_H
#define __RTC_H
#include "stm32f10x.h"
extern u8 cmd_stat,port;
extern u8 key1_sta,key3_sta;
extern int time_start,time_stop;
extern u8 receive;
void rtc_init(void);
#endif
rtc.c
#include "rtc.h"
#include "led.h"
#include "lcd.h"
u32 Time=23*3600+59*60+50;
void rtc_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(39999);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
RTC_SetCounter(Time);
RTC_WaitForLastTask();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
if(RTC_GetCounter()==86399)
{
RTC_SetCounter(0x0);
//led_ctl(LED7,0);
Time=0;
}
if(cmd_stat==1)
{
if(RTC_GetCounter()==time_start)
{
if(port!=0)
{
if(port==1)
{
TIM2->CCER|=1<<4;
key1_sta=1;
}
else if(port==2)
{
TIM2->CCER|=1<<8;
key3_sta=1;
}
}
}
else if(RTC_GetCounter()==time_stop)
{
if(port!=0)
{
if(port==1)
{
TIM2->CCER&=~(1<<4);
key1_sta=0;
}
else if(port==2)
{
TIM2->CCER&=~(1<<8);
key3_sta=0;
}
}
receive=0;
cmd_stat=0;
port=0;
}
}
/* Clear Interrupt pending bit */
RTC_ClearITPendingBit(RTC_IT_SEC);
}
}