📄 main.c
字号:
//-----------------------------------------------------------------------------
// MAIN.C
//-----------------------------------------------------------------------------
// Author Zhou Ke
// Email zhoukesec@163.com
// Target: FRAM_UART
// Tool chain: Keil C51 8.02 / Keil EVAL C51
// Project Name: FRAM_UART
//
//
// Release 1.0
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "stc12c2052.h"
#include "main.h"
#include "fram.h"
//-----------------------------------------------------------------------------
//Global variables
//-----------------------------------------------------------------------------
volatile unsigned char tx_buffer[8];
volatile unsigned char rx_buffer[4];
volatile unsigned char tmp_buffer[4];
volatile unsigned char system_status;
volatile bit tx_flag;
volatile bit save_flag;
volatile bit new_flag;
volatile unsigned char system_clock;
volatile unsigned char uart_time;
/*------------------------------------------------------------------------------
Function Name : main
Description : the main function of the project
Parameters :
Input : none
Output : none
Return : none
Notes :
------------------------------------------------------------------------------*/
main()
{
/*Init timer and uart*/
SCON = 0x50;
TMOD |= 0x21;
TH1 = 0xFD; //9600 bps @11.0592MHz
TL1 = 0xFD;
TH0 = 0x4C; //0.05 second @11.0592MHz
TL0 = 0x00;
/*Init SPI*/
SPCTL = 0xD0;
SPSTAT = 0xC0;
delay();
/*Init variables*/
system_status = 0;
tx_flag = 0;
save_flag = 0;
new_flag = 0;
system_clock = 0;
uart_time = 0;
/*read fram data to tx buffer*/
fram_read_data();
tx_buffer[0] = 0xff;
tx_buffer[1] = 0x20;
tx_buffer[7] = 0xff;
Enable_Watchdog();
/*start timer and enable interrupts*/
EA = 1;
ES = 1;
ET0 = 1;
TI = 0;
RI = 0;
TR0 = 1;
TR1 = 1;
/*main loop*/
while(1){
if(tx_flag){
uart_send_data();
tx_flag = 0;
}
if(save_flag){
fram_write_data();
save_flag = 0;
}
Feed_Watchdog();
}//end of while
}
/*------------------------------------------------------------------------------
Function Name : Timer0_ISR
Description : interrupt handler of Timer0
Parameters :
Input : none
Output : none
Return : none
Notes :
------------------------------------------------------------------------------*/
void Timer0_ISR(void) interrupt 1
{
if(!tx_flag){
system_clock ++;
if(system_clock >= 20){
tx_flag = 1;
system_clock = 0;
LED = ~LED;
}
}
//uart packet timeout, return to status 0
uart_time ++;
if(uart_time >= 5){
uart_time = 0;
system_status = 0;
}
/* reload value */
TH0 = 0x4C;
TL0 = 0x00;
}
/*------------------------------------------------------------------------------
Function Name : Uart_ISR
Description : interrupt handler of Uart
Parameters :
Input : none
Output : none
Return : none
Notes :
------------------------------------------------------------------------------*/
void Uart_ISR(void) interrupt 4
{
unsigned char tmp;
if(RI){
tmp = SBUF;
RI = 0;
uart_time = 0;
switch(system_status){
case 0:
if(tmp == 0xff){
system_status = 1;
}
break;
case 1:
if(tmp == 0x20){
system_status = 2;
}
else if(tmp == 0xff){
system_status = 1;
}
else{
system_status = 0;
}
break;
case 2:
rx_buffer[0] = tmp;
system_status = 3;
break;
case 3:
rx_buffer[1] = tmp;
system_status = 4;
break;
case 4:
rx_buffer[2] = tmp;
system_status = 5;
break;
case 5:
rx_buffer[3] = tmp;
system_status = 6;
break;
case 6:
if(((rx_buffer[3] + rx_buffer[2] + rx_buffer[1] + rx_buffer[0]) & 0xff) == tmp){
system_status = 7;
}else if(tmp == 0xff){
system_status = 1;
}else{
system_status = 0;
}
break;
case 7:
if(0xff == tmp){
if((rx_buffer[0] != tx_buffer[2]) || (rx_buffer[1] != tx_buffer[3]) || (rx_buffer[2] != tx_buffer[4]) || (rx_buffer[3] != tx_buffer[5])){
//Save to tmp buffer for writing to fram
tmp_buffer[0] = rx_buffer[0];
tmp_buffer[1] = rx_buffer[1];
tmp_buffer[2] = rx_buffer[2];
tmp_buffer[3] = rx_buffer[3];
new_flag = 1;
save_flag = 1;
}
}
system_status = 0;
break;
default:
break;
}//end of "switch"
}//end of "if RI "
}
/*------------------------------------------------------------------------------
Function Name : uart_send_data
Description : Send 7 bytes to host from uart
Parameters :
Input : none
Output : none
Return : none
Notes :
------------------------------------------------------------------------------*/
void uart_send_data(void)
{
unsigned char i;
//change tx data buffer
if(new_flag){
tx_buffer[2] = tmp_buffer[0];
tx_buffer[3] = tmp_buffer[1];
tx_buffer[4] = tmp_buffer[2];
tx_buffer[5] = tmp_buffer[3];
tx_buffer[6] = ((tmp_buffer[3] + tmp_buffer[2] + tmp_buffer[1] + tmp_buffer[0]) & 0xff);
new_flag = 0;
}
for(i=0; i<8; i++){
SBUF = tx_buffer[i];
while (!TI);
TI = 0;
delay();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -