main.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 118 行
C
118 行
/*
02/01/2006: Petre M.
-this program uses the watchdog timer (set at 2sec period) to send character
strings using the UART whenever the watchdog interrupts are generated
*/
#include "ioADE7169F16.h"
//the character '0' at the end of the string shows the end of the message
__code const char Start_Message[]="\r\nCode is starting\r\n\0";
__code const char Watchdog_ISR_Message[]="\r\nReached watchdog ISR due to watchdog timeout\r\n\0";
__idata char b;
void uart_init(void);
void watchdog_init(void);
void send_string(__idata char __code const *Message_ptr);
#pragma vector=0x5b
__interrupt void WATCHDOG_ISR(void)
{
send_string(&Watchdog_ISR_Message[0]);
}
void main( void )
{
watchdog_init();
uart_init();
send_string(&Start_Message[0]);
while(1);
}
//this message sends the
void send_string(char __code const *Message_ptr) {
char a;
a=*Message_ptr;
while (a!=0) {
SCON_bit.TI=0; //make sure this bit is cleared
SBUF = a; //send the character on the UART
Message_ptr++;
//wait until TI bit becomes 1
while (SCON_bit.TI==0);
// asm("nop");
// asm("nop");
// asm("nop");
// asm("nop");
a=*Message_ptr;
}
return;
}
//this function initializes the watchdog to 2 sec
void watchdog_init(void) {
//disable interrupts
IE_bit.EA=0;
WDCON_bit.WDWR = 1;//before writing to WDCON, this bit must be 1
/*WDCON=0111=2sec period
1=watchdog generates an interrupt when times out
0=status bit
1=watchdog enabled
1=watchdog write enable bit
*/
WDCON = 0x7b;
//enable interrupts
IE_bit.EA=1;
return;
}
//this routine initializes the UART for 9600 baud
void uart_init(void) {
/*01=Mode 1=8 bit UART, variable rate
0=RI is set as soon as the data byte is received
0=serial port reception disabled
0=this bit represents the 9th data bit transmitted in Modes 2 and 3
0=this bit represents the stop bit received in Mode 1 case
0=Tx INT flag
0=Rx INT flag
*/
SCON = (SCON & 0x00) | 0x40;
//for 9600baud@4.096MHz, DIV=4 and SBAUDF=0xab, real freq=9570baud
SBAUDT = 0x04;
SBAUDF = 0xab;
return;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?