各位大侠,新手求助,我的uart中断程序没法接收是什么情况,对uart进入接收中断的机制很是不了解啊!
//UART
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F340.h> // SFR declarations
#include <stdio.h> // printf is declared here
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for the 'F34x
//-----------------------------------------------------------------------------
sfr16 TMR2 = 0xCC; // Timer2 low and high bytes together
//-----------------------------------------------------------------------------
// User-defined types, structures, unions etc
//-----------------------------------------------------------------------------
#ifndef BYTE
#define BYTE unsigned char
#endif
#ifndef UINT
#define UINT unsigned int
#endif
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 24000000 // Internal oscillator frequency in Hz
sbit LED_1 = P3^4; // LED='1' means ON
sbit LED_2 = P3^5;
#define IN_LEN 16
unsigned char in_buffer[IN_LEN];
unsigned int inhead,intail;
bit infull;
bit readflag;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void PCA0_Init (void);
void OSCILLATOR_Init (void);
void PORT_Init (void);
void TIMER2_Init (void);
void UART0_Init (void);
void Init_Device (void);
void UART_interrupt( void );
void UART0_Send_data(unsigned char Send_data)
{
SBUF0=Send_data;
while(!TI0);
TI0=0;
}
void UART0_Send_Longdata(unsigned char *str, unsigned int Stringlen)
{
unsigned int i = 0 ;
do {
UART0_Send_data(*(str+i));
i++;
} while (i<Stringlen);
}
void UART_interrupt(void) interrupt 4
{
if (RI0)
{
RI0 = 0; // 清接收中断标志RI0
in_buffer[intail] = SBUF0; // 从串口接收一个字符
intail = (intail+1)%(IN_LEN-1); //向输入缓冲写数据
if( intail == inhead ) infull = 1 ; //输入缓冲满
readflag = 1 ;
}
}
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
char rx[8];
bit flag = 1;
xdata char c[]="enter a string :\n";
xdata char r[]="controll bits is wrong!\nenter again:";
Init_Device ();
LED_1 = 0;
LED_2 = 0;
UART0_Send_Longdata(c,sizeof(c));
while(1){
rx[0]= 0;
if(readflag){
int i=0;
for( ; inhead%(IN_LEN-1) < intail ; inhead = (inhead+1)%(IN_LEN-1) ){
rx[i]=in_buffer[inhead%(IN_LEN-1)];
i++;
}
UART0_Send_Longdata(rx,sizeof(rx));
UART0_Send_Longdata(r,sizeof(r));
}
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function disables the watchdog timer.
//
//-----------------------------------------------------------------------------
void PCA0_Init (void)
{
PCA0MD &= ~0x40;
PCA0MD = 0x00;
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock at 24 MHz (intosc->clkmul/2).
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int i = 0;
OSCICN = 0x83;
CLKMUL = 0x80;
for (i = 0; i < 20; i++); // Wait 5us for initialization
CLKMUL |= 0xC0;
while ((CLKMUL & 0x20) == 0);
CLKSEL = 0x02;
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P1MDOUT = 0x0D; //spi sck miso mosi cs配置
P2MDOUT = 0x00;
P2SKIP = 0x03;
P3MDOUT = 0x30; //LED D1 D2
P3SKIP = 0x0C;
XBR0 = 0x03; //spi i/o使能
XBR1 = 0x40; //7:弱上拉使能,6:交叉开关使能
}
//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Initializes Timer2 to be clocked by SYSCLK for use as a delay timer.
//
//-----------------------------------------------------------------------------
void TIMER2_Init (void)
{
CKCON |= 0x10;
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configures the UART0 using Timer1, for <BAUDRATE> and 8-N-1. Once this is
// set up, the standard printf function can be used to output data.
//
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1)
{
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else
{
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
ES0 = 1 ;
REN0 = 1;
inhead = 0 ;
intail = 0;
//UART_interrupt();
}
//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Calls all device initialization functions.
//
//-----------------------------------------------------------------------------
void Init_Device (void)
{
PCA0_Init ();
OSCILLATOR_Init ();
PORT_Init ();
TIMER2_Init ();
UART0_Init ();
//UART_interrupt();
}
|