129 lines
3.5 KiB
C
129 lines
3.5 KiB
C
#include "config.h"
|
||
#include "gpio.h"
|
||
#include "UART.h"
|
||
#include "string.h"
|
||
#include "UART_Set.h"
|
||
#include "pwm_control.h"
|
||
#include "Start_Init.h"
|
||
#include "key.h"
|
||
#include "WDT.h"
|
||
/************* 功能说明 **************
|
||
|
||
本例程基于STC8H8K64U为主控芯片的实验箱8进行编写测试,STC8G、STC8H系列芯片可通用参考.
|
||
|
||
双串口全双工中断方式收发通讯程序。
|
||
|
||
通过PC向MCU发送数据, MCU收到后通过串口把收到的数据原样返回, 默认波特率:115200,N,8,1.
|
||
|
||
通过开启 UART.h 头文件里面的 UART1~UART4 定义,启动不同通道的串口通信。
|
||
|
||
用定时器做波特率发生器,建议使用1T模式(除非低波特率用12T),并选择可被波特率整除的时钟频率,以提高精度。
|
||
|
||
下载时, 选择时钟 22.1184MHz (可以在配置文件"config.h"中修改).
|
||
|
||
|
||
/******************* IO配置函数 *******************/
|
||
|
||
|
||
void GPIO1_config(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure; //结构定义
|
||
|
||
GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
|
||
GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
|
||
GPIO_Inilize(GPIO_P3,&GPIO_InitStructure); //初始化
|
||
}
|
||
|
||
/*************** 串口初始化函数 *****************/
|
||
void UART1_config(void)
|
||
{
|
||
COMx_InitDefine COMx_InitStructure; //结构定义
|
||
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
|
||
COMx_InitStructure.UART_BRT_Use = BRT_Timer1; //使用波特率, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
|
||
COMx_InitStructure.UART_BaudRate = 115200; //波特率, 一般 110 ~ 115200
|
||
COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
|
||
COMx_InitStructure.BaudRateDouble = DISABLE; //波特率加倍, ENABLE或DISABLE
|
||
COMx_InitStructure.UART_Interrupt = ENABLE; //中断允许, ENABLE或DISABLE
|
||
COMx_InitStructure.UART_Priority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
|
||
COMx_InitStructure.UART_P_SW = UART1_SW_P30_P31; //切换端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
|
||
UART_Configuration(UART1, &COMx_InitStructure); //初始化串口1 UART1,UART2,UART3,UART4
|
||
|
||
//PrintString1("STC8H8K64U UART1 Test Programme!\r\n"); //UART1发送一个字符串
|
||
}
|
||
|
||
/********************WDT INT配置 ********************/
|
||
void WDT_config(void)
|
||
{
|
||
WDT_InitTypeDef WDT_InitStructure; //结构定义
|
||
|
||
WDT_InitStructure.WDT_Enable = ENABLE; //中断使能 ENABLE或DISABLE
|
||
WDT_InitStructure.WDT_IDLE_Mode = WDT_IDLE_STOP; //IDLE模式是否停止计数 WDT_IDLE_STOP,WDT_IDLE_RUN
|
||
WDT_InitStructure.WDT_PS = WDT_SCALE_32; //看门狗定时器时钟分频系数 WDT_SCALE_2,WDT_SCALE_4,WDT_SCALE_8,WDT_SCALE_16,WDT_SCALE_32,WDT_SCALE_64,WDT_SCALE_128,WDT_SCALE_256
|
||
WDT_Inilize(&WDT_InitStructure); //初始化
|
||
}
|
||
|
||
u8 count_flag=0;
|
||
void main(void)
|
||
{
|
||
GPIO_config();
|
||
UART3_config();
|
||
Start_Init();
|
||
pwm_config();
|
||
Timer2_Init_1ms();
|
||
Key_Init();
|
||
GPIO1_config();
|
||
UART1_config();
|
||
WDT_config(); //看门狗,629ms复位
|
||
|
||
PCON &= ~POF; //清除LVD中断标志位
|
||
RSTCFG = 0x41; //LVD:2.4V低压复位
|
||
EA = 1;
|
||
|
||
if(debug)
|
||
{
|
||
PrintString1("MCU Start");
|
||
}
|
||
|
||
while (1)
|
||
{
|
||
WDT_Clear(); //清狗
|
||
count_flag++;
|
||
|
||
//为了保证某电压2.2V以上
|
||
if(count_flag==1)
|
||
{
|
||
P10=1;
|
||
}
|
||
if(count_flag==8)
|
||
{
|
||
P10=0;
|
||
}
|
||
if(count_flag==10)
|
||
{
|
||
count_flag=0;
|
||
}
|
||
|
||
Usart_judge_Data();
|
||
|
||
Usart_Deal_Data();
|
||
|
||
deal_command1();
|
||
|
||
deal_command2();
|
||
|
||
// checkpwm();
|
||
|
||
show_light();
|
||
|
||
Usart_answer();
|
||
|
||
Key_ScanTask();
|
||
|
||
KEY_TEST();
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|