本届试题看似简单,实则暗藏杀机,相比往年的题目,近两年来嵌入式难度有所提高,虽说考的模块不算多,但无疑增加了考察的灵活度,本届赛题即是典型,比如考察按键的长短按测量,我们需要检测按键按下的时间......
main.c
#include "key.h"
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "at24c02.h"
#include "tim.h"
unsigned char ucNo = 1, ucLed = 1;
unsigned char ucState, ucStr[21];
unsigned char ucHour, ucMin, ucSec, ucKey_Long;
unsigned long ulTick_ms, ulSec, ulKey_Time;
void KEY_Proc(void);
void LCD_Proc(void);
void B3_Proc(void);
int main(void)
{
SysTick_Config(72000);
KEY_Init();
LED_Init();
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
i2c_init();
TIM3_OCInit_7591();
i2c_read(ucStr, 0, 3);
ucHour = ucStr[0];
ucMin = ucStr[1];
ucSec = ucStr[2];
if(ucHour > 24) ucHour = 0;
if(ucMin > 60) ucMin = 0;
if(ucSec > 60) ucSec = 0;
while(1)
{
KEY_Proc();
LCD_Proc();
}
}
void KEY_Proc(void)
{
unsigned char ucKey_Val;
ucKey_Val = KEY_Scan();
switch(ucKey_Val)
{
case 1: // B1: ???????
if(!ucState)
{
if(++ucNo == 6)
ucNo = 1;
i2c_read(ucStr, 3*(ucNo-1), 3);
ucHour = ucStr[0];
ucMin = ucStr[1];
ucSec = ucStr[2];
}
break;
case 2: // B2: ???????
if(!ucState)
ucState = 1;
else
if(++ucState == 4)
ucState = 1;
break;
case 3: // B3: ???
B3_Proc();
break;
case 4: // B4: ???
if(!ucState)
{
i2c_read(ucStr, 3*(ucNo-1), 3);
ucHour = ucStr[0];
ucMin = ucStr[1];
ucSec = ucStr[2];
}
if(ucState < 4)
ulSec = (long)ucHour*3600 + ucMin*60 + ucSec;
if(ucState != 4)
{
TIM_CCxCmd(TIM3, TIM_Channel_1, ENABLE);
ucState = 4;
}
else
ucState = 5;
}
if(ulTick_ms-ulKey_Time > 800)
{
switch(ucKey_Long)
{
case 2:
if(ucState == 1 || ucState == 2 || ucState == 3)
{
ucStr[0] = ucHour;
ucStr[1] = ucMin;
ucStr[2] = ucSec;
i2c_write(ucStr, 3*(ucNo - 1), 3);
ucState = 0;
}
break;
case 3:
B3_Proc();
break;
case 4:
if(ucState == 4 || ucState == 5)
ucState = 0;
}
}
}
void B3_Proc(void)
{
switch(ucState)
{
case 1:
if(++ucHour > 23)
ucHour = 0;
break;
case 2:
if(++ucMin > 59)
ucMin = 0;
break;
case 3:
if(++ucSec > 59)
ucSec = 0;
}
}
void LCD_Proc(void)
{
sprintf((char*)ucStr, " No.%1u", ucNo);
LCD_DisplayStringLine(Line3, ucStr);
if(ucState == 1) LCD_SetBackColor(Red);
LCD_DisplayChar(Line5, 224, ucHour/10+0x30);
LCD_DisplayChar(Line5, 208, ucHour%10+0x30);
LCD_SetBackColor(Blue);
LCD_DisplayChar(Line5, 192, ':');
if(ucState == 2) LCD_SetBackColor(Red);
LCD_DisplayChar(Line5, 176, ucMin/10+0x30);
LCD_DisplayChar(Line5, 160, ucMin%10+0x30);
LCD_SetBackColor(Blue);
LCD_DisplayChar(Line5, 144, ':');
if(ucState == 3) LCD_SetBackColor(Red);
LCD_DisplayChar(Line5, 128, ucSec/10+0x30);
LCD_DisplayChar(Line5, 112, ucSec%10+0x30);
LCD_SetBackColor(Blue);
switch(ucState)
{
case 0:
sprintf((char*)ucStr, " Standby.");
break;
case 1:
case 2:
case 3:
sprintf((char*)ucStr, " Setting.");
break;
case 4:
sprintf((char*)ucStr, " Running.");
break;
case 5:
sprintf((char*)ucStr, " Pause. ");
}
LCD_DisplayStringLine(Line7, ucStr);
}
//SysTick??????????:
void SysTick_Handler(void)
{
ulTick_ms++;
if(ucState == 4)
{
if(ulTick_ms%500 == 0)
{
ucLed ^= 1;
LED_Disp(ucLed);
}
if(ulTick_ms%1000 == 0)
{
if(ulSec--)
{
ucHour = ulSec / 3600;
ucMin = ulSec % 3600 / 60;
ucSec = ulSec % 3600 % 60;
}
else
ucState = 0;
}
}
else
{
TIM_CCxCmd(TIM3, TIM_Channel_1, DISABLE);
LED_Disp(0);
}
}
key.c
#include "key.h"
// KEY接口初始化
void KEY_Init(void)
{
//GPIO_InitTypeDef GPIO_InitStructure;
// 允许GPIOA和GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* PA0和PA8浮空输入(复位状态,可以省略)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); */
/* PB1和PB2浮空输入(复位状态,可以省略)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure); */
}
// KEY扫描
unsigned char KEY_Scan(void)
{
unsigned char ucKey_Val = 0;
// 判断B1和B2是否按下
if(~GPIO_ReadInputData(GPIOA) & 0x101)
{
Delay_KEY(10); // 延时10ms消抖
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
ucKey_Val = 1;
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
ucKey_Val = 2;
}
// 判断B3和B4是否按下
else if(~GPIO_ReadInputData(GPIOB) & 6)
{
Delay_KEY(10); // 延时10ms消抖
if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
ucKey_Val = 3;
if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2))
ucKey_Val = 4;
}
if(ucKey_Val != ucKey_Long)
{
ucKey_Long = ucKey_Val;
ulKey_Time = ulTick_ms;
return ucKey_Val;
}
return 0;
}
void Delay_KEY(unsigned int ms)
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<7992; j++); // SYSCLK = 72MHz
// for(j=0; j<1598; j++); // SYSCLK = 8MHz
}
at24c02.c(EEPROM)
/*
程序说明: CT117E嵌入式竞赛板GPIO模拟I2C总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT117E嵌入式竞赛板
日 期: 2011-8-9
*/
#include "stm32f10x.h"
/** I2C 总线接口 */
#define I2C_PORT GPIOB
#define SDA_Pin GPIO_Pin_7
#define SCL_Pin GPIO_Pin_6
#define FAILURE 0
#define SUCCESS 1
//配置SDA信号线为输入模式
void SDA_Input_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
//配置SDA信号线为输出模式
void SDA_Output_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
//
void SDA_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(I2C_PORT,SDA_Pin);
} else {
GPIO_ResetBits(I2C_PORT,SDA_Pin);
}
}
//
void SCL_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(I2C_PORT,SCL_Pin);
} else {
GPIO_ResetBits(I2C_PORT,SCL_Pin);
}
}
//
uint8_t SDA_Input()
{
return GPIO_ReadInputDataBit( I2C_PORT, SDA_Pin);
}
//延时程序
void delay1(unsigned int n)
{
unsigned int i;
for ( i=0;i<n;++i);
}
//I2C总线启动
void I2CStart(void)
{
SDA_Output(1); delay1(500);
SCL_Output(1); delay1(500);
SDA_Output(0); delay1(500);
SCL_Output(0); delay1(500);
}
//I2C总线停止
void I2CStop(void)
{
SCL_Output(0); delay1(500);
SDA_Output(0); delay1(500);
SCL_Output(1); delay1(500);
SDA_Output(1); delay1(500);
}
//等待应答
unsigned char I2CWaitAck(void)
{
unsigned short cErrTime = 5;
SDA_Input_Mode(); delay1(500);
SCL_Output(1); delay1(500);
while(SDA_Input())
{
cErrTime--;
delay1(500);
if (0 == cErrTime)
{
SDA_Output_Mode();
I2CStop();
return FAILURE;
}
}
SDA_Output_Mode();
SCL_Output(0); delay1(500);
return SUCCESS;
}
//发送应答位
void I2CSendAck(void)
{
SDA_Output(0); delay1(500);
SCL_Output(1); delay1(500);
SCL_Output(0); delay1(500);
}
//
void I2CSendNotAck(void)
{
SDA_Output(1); delay1(500);
SCL_Output(1); delay1(500);
SCL_Output(0); delay1(500);
}
//通过I2C总线发送一个字节数据
void I2CSendByte(unsigned char cSendByte)
{
unsigned char i = 8;
while (i--)
{
SCL_Output(0); delay1(500);
SDA_Output(cSendByte & 0x80); delay1(500);
cSendByte += cSendByte;
SCL_Output(1);delay1(500);
}
SCL_Output(0); delay1(500);
}
//从I2C总线接收一个字节数据
unsigned char I2CReceiveByte(void)
{
unsigned char i = 8;
unsigned char cR_Byte = 0;
SDA_Input_Mode();
while (i--)
{
cR_Byte += cR_Byte;
SCL_Output(0); delay1(500);
SCL_Output(1); delay1(500);
cR_Byte |= SDA_Input();
}
SCL_Output(0); delay1(500);
SDA_Output_Mode();
return cR_Byte;
}
//I2C总线初始化
void i2c_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = SDA_Pin | SCL_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // **
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
// AT24C02???
void i2c_read(unsigned char* pucBuf, unsigned char ucAddr,
unsigned char ucNum)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(ucAddr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(ucNum--)
{
*pucBuf++ = I2CReceiveByte();
if(ucNum)
I2CSendAck();
else
I2CSendNotAck();
}
I2CStop();
}
// AT24C02???
void i2c_write(unsigned char* pucBuf, unsigned char ucAddr,
unsigned char ucNum)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(ucAddr);
I2CWaitAck();
while(ucNum--)
{
I2CSendByte(*pucBuf++);
I2CWaitAck();
}
I2CStop();
delay1(500);
}
tim.c
#include "tim.h"
// TIM1??2(PA9)???????????
void TIM1_OCInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 999;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct.TIM_Pulse = 250;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1, &TIM_OCInitStruct);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
// TIM2??2(PA1)??????????????????
void TIM2_ICInit(void)
{
//GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/*
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
*/
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStruct);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_Cmd(TIM2, ENABLE);
}
// TIM2????????????????
unsigned char TIM2_Cap(unsigned int* puiTim_Val)
{
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC2))
{
puiTim_Val[0] = TIM_GetCapture2(TIM2)+1;
puiTim_Val[1] = TIM_GetCapture1(TIM2)+1;
return 1;
}
return 0;
}
void TIM2_OCInit_7272(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 999;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = 500;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStruct);
TIM_Cmd(TIM2, ENABLE);
}
void TIM3_ICInit_7272(void)
{
//GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/*
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
*/
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0;
TIM_ICInit(TIM3, &TIM_ICInitStruct);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_Cmd(TIM3, ENABLE);
}
// TIM3????????????????
unsigned int TIM3_Cap(void)
{
if(TIM_GetFlagStatus(TIM3, TIM_FLAG_CC2))
return TIM_GetCapture2(TIM3)+1;
else
return 0;
}
void TIM3_OCInit_7381(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 999;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct.TIM_Pulse = 500;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStruct);
TIM_OC2Init(TIM3, &TIM_OCInitStruct);
TIM_Cmd(TIM3, ENABLE);
}
void TIM2_ICInit_7482(void)
{
//GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
//NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/*
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
*/
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_3;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
TIM_Cmd(TIM2, ENABLE);
/*?????
TIM_ITConfig(TIM2, TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);*/
}
void TIM3_OCInit_7482(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
//NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = 1600;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStruct);
TIM_OC2Init(TIM3, &TIM_OCInitStruct);
TIM_Cmd(TIM3, ENABLE);
/*?????
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);*/
}
void TIM3_OCInit_7591(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 999;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct.TIM_Pulse = 800;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStruct);
TIM_Cmd(TIM3, ENABLE);
}
中断服务函数
static unsigned char ms2 = 0;
static unsigned char ms3 = 3;
unsigned char switch_led = 0;
extern unsigned char ledFlag;
extern unsigned char ledEnable;
//
void SysTick_Handler(void)
{
TimingDelay--;
if(++ms3 == 200){
ms3 = 0;
switch_led = ~switch_led;
}
if(++ms2 == 10){
ms2 = 0;
Key_Scan();
}
}
extern unsigned char TimeDisplay;
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET){
TimeDisplay = 1; //显示更新标志
RTC_ClearITPendingBit(RTC_IT_SEC);
}
}
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
char rx_buff[20];
char rx_counter = 0;
char rx_over = 0;
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET){
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
rx_buff[rx_counter] = USART_ReceiveData(USART2);
if((rx_counter == 19) || (rx_buff[rx_counter] == '\n')){
rx_over = 1;
rx_counter = 0;
}
else{
rx_counter++;
}
}
}