程序运行,用示波器测试串口TX端,发送数据有不发送现象,大家看看什么原因,指点指点,波特率越高不发送数据越多,程序:
#include <c8051f340.h>
#include <intrins.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned int FLADDR;
sfr16 TMR2RL=0xCA; // Timer2 reload value
sfr16 TMR2 = 0xCC; // Timer2 counter
#define SYSCLK 12000000 // SYSCLK frequency in 12MHz
#define BAUDRATE0 4800 // Baud rate of UART0 in bps(与PC机通信)
#define TIMER0_RELOAD_HIGH 0x44 // Timer0 High register (65536-TIMER0_COUNT)/256;
#define TIMER0_RELOAD_LOW 0x80 // Timer0 Low register (65536-TIMER0_COUNT)%256;
sbit led = P1^7;
uchar ab=0;
unsigned char data send_num;
unsigned char idata sendbuff[6];
void SYSCLK_Init (void);
void PORT_Init(void); // initialize crossbar and GPIO
//void Timer0_Init(void); // initialize Timer0
void Timer2_Init(void); // initialize Timer2
//void ADC0_Init (void); // initialize ADC0
void UART0_Init(void); // initialize UART0
void com_send(void); // 串口数据处理
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (Disable Watchdog timer)
// mpu_pwr_p = on;
PORT_Init(); // initialize crossbar and GPIO
SYSCLK_Init (); // Initialize SYSCLK
//Timer0_Init(); // initialize Timer0
Timer2_Init(); // initialize Timer2
//ADC0_Init(); // initialize ADC0
UART0_Init();
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
EA = 1;
while(1);
}
void SYSCLK_Init (void)
{
uint i=0;
VDM0CN=0x80; // 启动电源监视器
for(i=0;i<1000;i++);//等待电压稳定
RSTSRC=0x22; //设置上电复位
OSCICN=0x83; //频率设置
// RSTSRC=0x00;
}
//----------------------------------PORT_Init---------------------------------
void PORT_Init (void)
{
//P0MDIN &= ~0xC0; // P0.6, P0.7 are analog
//P0SKIP |= 0xC0; // P0.6, P0.7 skipped in the Crossbar
P0MDOUT |= 0x10; // P0.0 is push-pull
//P1MDIN &= ~0x04; // set P1.2 as an analog input
P1MDOUT |= 0x80; // P1.3 is push-pull
//P1SKIP |= 0x04;
//P1 |= 0xc2;
// P2MDIN = 0xff;
// P2MDOUT = 0xf0;
// P3MDOUT &= 0x00;
// P4MDOUT &= 0x00;
XBR0 |= 0x01;
XBR1 |= 0x41; // Enable Crossbar and weak pull-ups Route CEX0 to P0.0,
}
//-------------------------------- UART0_Init ---------------------------------
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
SCON0 = 0x10;
if (SYSCLK/BAUDRATE0/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE0/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx 使用SYSCLK
} else if (SYSCLK/BAUDRATE0/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE0/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE0/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE0/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else if (SYSCLK/BAUDRATE0/2/256 < 48) {
TH1 = -(SYSCLK/BAUDRATE0/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
} else {
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
IP |= 0x10; // Make UART high priority
ES0 = 1; // Enable UART0 interrupts
}
//-------------------------------- Timer2_Init --------------------------------
void Timer2_Init (void)
{
CKCON &= ~0x30; // Timer2 uses SYSCLK/12
TMR2CN &= ~0x09;
T2SOF = 0;
TMR2RL = 15536;
TMR2 = TMR2RL; // set to reload immediately
TR2 = 1;
// PT2=1; // start Timer2
ET2=1;
}
void Timer2_ISR (void) interrupt 5
{
TF2H = 0;
++ab;
if(ab==2)
{
ab=0;
com_send();
//send_num=1;
//send_flg=1;
//SBUF0=sendbuff[0]; //启动发送
led=~led;
} // Toggle the LED
// Reset Interrupt
}
void com_send()
{
unsigned int kc;
kc=0;
sendbuff[0]=0x80; kc+=sendbuff[0];
sendbuff[1]=0x80; kc+=sendbuff[1];
sendbuff[2]=0x00; kc+=sendbuff[2];
sendbuff[3]=0x00; kc+=sendbuff[3];
sendbuff[4]=0x7f&(kc%100);
sendbuff[5]=0x7f&(kc/100);
send_num=1;
//send_flg=1;
SBUF0=sendbuff[0]; //启动发送
}
//------------------------------- UART0_Interrupt -----------------------------
void UART0_Interrupt (void) interrupt 4
{
// bit send_over;
if (TI0 == 1)
{
TI0 = 0;
if (send_num>=6)
{
send_num=0;// 如果send_num>7,数据发送完毕,退出
}
else
{
SBUF0 = sendbuff[send_num];
send_num++;
}
}
if (RI0 == 1)
{
RI0 = 0; // Clear interrupt flag
//Byte = SBUF0; // Read a character from UART
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
-----------------------------------------------------------------------
SBUF0 = sendbuff[send_num];
后面要加是否发送完了的检查,否则会丢弃数据的!
一周热门 更多>