我用gm8125扩展c8051f040的串口,单路通讯时没有问题,但多路工作时却出错,我把程序发出来,哪位高手帮着看看,谢谢了!
#include <C8051F040.h> // SFR declarations
#include <stdio.h>
sfr16 RCAP2 = 0xCA;
sfr16 TMR2 = 0xCC;
#define SYSTEMCLOCK 11059200 //系统晶振频率(外部晶振)
#define DELAY_TIME 60000 //Delay time
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long int DWORD;
sbit MS=P5^0; //GM8125 工作模式控制
sbit RESET=P5^1; //GM8125 复位引脚控制
sbit STADD0=P4^0; //GM8125 发送地址线0
sbit STADD1=P4^1; //GM8125 发送地址线1
sbit STADD2=P4^2; //GM8125 发送地址线2
sbit SRADD0=P4^3; //GM8125 接受地址线0
sbit SRADD1=P4^4; //GM8125 接受地址线1
sbit SRADD2=P4^5; //GM8125 接受地址线2
//---------------------------------------------------------------------------------------------------
// 函数声明
//---------------------------------------------------------------------------------------------------
//系统初始化
void OSCILLATOR_Init (void); //晶振初始化程序
void PORT_Init (void); //端口初始化程序
void SYSTEM_INTT(void); //系统初始化程序
void Interrupt_Priority(void); //系统中断优先级设置程序
void SendData(BYTE SingleData); //往串口0中写入数据
void Delaytime(WORD Count);
void UART0_Init(long int BAUDRATE, int Type); //串口0初始化
//---------------------------------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------------------------------
void main (void)
{
SFRPAGE = CONFIG_PAGE;
WDTCN = 0xDE; // Disable watchdog timer
WDTCN = 0xAD;
Interrupt_Priority();
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (7200, 3); //母口工作波特率为7200
EA = 1;
RESET = 0; // gm8125系统复位
Delaytime(6000);
RESET = 1;
Delaytime(6000);
MS = 0; //GM8125 工作在写命令字模式下
STADD0 = 0; //置GM8125 命令字地址
STADD1 = 0;
STADD2 = 0;
Delaytime(6000);
SendData(0XF4); //设置GM8125 子串口波特率为19200bps,母串口波特率为115200bps
Delaytime(6000);
while(1)
{
MS = 0; //GM8125工作在多通道模式
STADD0 = 1; //
STADD1 = 0;
STADD2 = 0;
Delaytime(6000);
SendData(0x35);
}
}
void Delaytime(WORD Count)
{
xdata long i;
for(i=Count; i>0; i--)
{
}
}
//***************************************************************************************************
//*******************************系统初始化程序******************************************************
//***************************************************************************************************
//---------------------------------------------------------------------------------------------------
//系统初始化程序
//---------------------------------------------------------------------------------------------------
void SYSTEM_INTT(void)
{
SFRPAGE = CONFIG_PAGE;
WDTCN = 0xDE; // Disable watchdog timer
WDTCN = 0xAD;
Interrupt_Priority();
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART0_Init (); // Initialize UART0
EA = 1;
}
//---------------------------------------------------------------------------------------------------
// 系统中断优先级设置程序
//---------------------------------------------------------------------------------------------------
void Interrupt_Priority(void) //系统中断优先级设置程序
{
EIP1 = 0x00; //PCA中断高优先级
EIP2 = 0x40; //Uart1中断低优先级
IP = 0x08; //Uart0中断低优先级
}
//---------------------------------------------------------------------------------------------------
// 系统振荡器初始化函数(系统使用外部11兆晶振)
//---------------------------------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int i; // 软件定时器
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // Set SFR page
// Initialize external crystal oscillator to use 11 MHz crystal
OSCXCN = 0x67; // Enable external crystal osc.
for (i=0; i < 256; i++); // Wait at least 1ms
while (!(OSCXCN & 0x80)); // Wait for crystal osc to settle
CLKSEL = 0x01;
// Select external crystal as SYSTEMCLOCK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//---------------------------------------------------------------------------------------------------
// 端口初始化函数
//---------------------------------------------------------------------------------------------------
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // Set SFR page
XBR0 = 0x37; // 使能串口0 使能I2C 使能SPI
XBR1 = 0x00;
XBR2 = 0x44; // 使能串口1 使能全局弱上拉 使能交叉开关
P0MDOUT |= 0x01;
P0MDOUT = 0x01; // 设置串口0发送引脚(p0.0)为推免方式
P4MDOUT = 0xff; // 设置P4端口为推免方式
P5MDOUT = 0xff; // 设置P5端口为推免方式
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//---------------------------------------------------------------------------------------------------
// 串口0初始化
//---------------------------------------------------------------------------------------------------
void UART0_Init (long int BAUDRATE, int Type) //串口0初始化
{
char SFRPAGE_SAVE;
SFRPAGE_SAVE = SFRPAGE; // Preserve SFRPAGE
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Timer in 16-bit auto-reload up timer // mode
TMR2CF = 0x08; // SYSCLK is time base; no output;
// up count only
RCAP2 = - ((long) SYSTEMCLOCK/BAUDRATE/16);
TMR2 = RCAP2;
TR2= 1; // Start Timer2
SFRPAGE = UART0_PAGE;
if(Type == 1)
{
SCON0 = 0x50;
}
else if(Type == 3)
{
SCON0 = 0xd0;
}
SSTA0 = 0x15;
// Clear all flags; enable baud rate
// doubler (not relevant for these
// timers);
// Use Timer2 as RX and TX baud rate
// source;
ES0=1 ; //EA=1;ES0 = 1;
IP |= 0x10;
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//---------------------------------------------------------------------------------------------------
// 串口0写函数
//---------------------------------------------------------------------------------------------------
void SendData(BYTE SingleData) //往串口中写入数据
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = UART0_PAGE;
SBUF0 = SingleData; // Tleransmit to Hyperterminal
while(!TI0) ;
TI0=0;
SFRPAGE = SFRPAGE_SAVE;
}
//---------------------------------------------------------------------------------------------------
多路工作时向串口1发送0x55,发出来的确实0xfc,应该是初始化的问题,希望大家帮忙看看吧!谢谢 |