将汽车总线sent协议内容通过RS232方式输出。程序使用C8051F340芯片。
单片机源程序如下:
- //串口通迅实验,将该程序的HEX下载到开发板,用串口调试助手软件波特率设为9600
- //发送任意一个字符,单片机返回该字符的ASII码
- //-----------------------------------------------------------------------------
- // Includes
- //-----------------------------------------------------------------------------
- #include <c8051f340.h> // SFR declarations
- #include <stdio.h>
- //-----------------------------------------------------------------------------
- // Global CONSTANTS
- //-----------------------------------------------------------------------------
- #define SYSCLK 22118400 // SYSCLK frequency in Hz
- #define BAUDRATE 9600 // Baud rate of UART in bps
- sbit led1=P2^2;
- sbit led2=P2^3;
- sbit P20=P2^0;
- //-----------------------------------------------------------------------------
- // Function PROTOTYPES
- //-----------------------------------------------------------------------------
- void SYSCLK_Init (void);
- void UART0_Init (void);
- void PORT_Init (void);
- void Timer0_Init (int);
- void Ext_Interrupt_Init (void);
- void INT1_ISR (void);
- void Timer0_init (void);
- unsigned int data count,tick,tickTemp,i,type;
- unsigned int data dataR[20] ;
- unsigned short data Sent=0,i;
- bit data flagR,flag2,flagp01;
- //-----------------------------------------------------------------------------
- // MAIN Routine
- //-----------------------------------------------------------------------------
- void main (void)
- {
- flag2=0;
- PORT_Init(); // Initialize Port I/O
- PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
- SYSCLK_Init (); // Initialize Oscillator
- Ext_Interrupt_Init(); // enable)
- led1=1;
- led2=0;
- UART0_Init();
- Timer0_init();
- EA=1;
- while (1)
- {
- if(flag2||P20==0)
- {
- EA=0;
- printf("angle= %f\n",(dataR[1]*256+dataR[2]*16+dataR[3])*283.0/4095);
- led2=~led2;
- Sent=0;
- type=0;
- flag2=0;
- EA=1;
- }
- }
- }
- //-----------------------------------------------------------------------------
- // PORT_Init
- //-----------------------------------------------------------------------------
- //
- // P0.4 digital push-pull UART TX
- // P0.5 digital open-drain UART RX
- //
- //-----------------------------------------------------------------------------
- void PORT_Init (void)
- {
- P0MDIN &=~0xc0 ;
- P0MDOUT |= 0x10; // Enable UTX as push-pull output
- XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
- XBR1 = 0x40; // Enable crossbar and weak pull-ups
- P2MDOUT |= 0x0c;
- }
- //-----------------------------------------------------------------------------
- // SYSCLK_Init
- //-----------------------------------------------------------------------------
- //
- // Return Value : None
- // Parameters : None
- //
- // This routine initializes the system clock to use the internal oscillator
- // at its maximum frequency.
- // Also enables the Missing Clock Detector.
- //-----------------------------------------------------------------------------
- void SYSCLK_Init (void) //初始化系统时钟
- {
- //int i=0;
- unsigned short data dly=0,dlyb=12;
- //------------------------------------
- //使能外部振荡器
- OSCXCN |=0X67; //0110 0111
- dlyb=22;
- do {
- dly=120;
- while(dly--);
- }while(dlyb--);
- //等待1毫秒
- do{
- dly=100;
- while(dly--);
- }while((OSCXCN&0X80)!=0X80);
- //查询XTLVLD =>1。
- dly=100;
- while(dly--);
- CLKSEL |=0X01;
- // OSCICN = 0x00;
- }
- void Timer0_init (void)
- {
-
- CKCON |=0X04;
- TL0 = TH0=190; // Init Timer1
- TMOD &= ~0x0f; // TMOD: timer 1 in 8-bit autoreload
- TMOD |= 0x02;
- TR0 = 1; // START Timer1
- ET0=1;
- }
- //-----------------------------------------------------------------------------
- // UART0_Init
- //-----------------------------------------------------------------------------
- //
- // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
- //-----------------------------------------------------------------------------
- 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
-
- //TH1 = 256-(SYSCLK/BAUDRATE/2/12);
- TH1=256-96;
- // CKCON &=~0x0c;
- // CKCON |= 0x00; // T1M = 1; SCA1:0 = 00
-
- TL1 = TH1; // Init Timer1
- TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
- TMOD |= 0x20;
- TR1 = 1; // START Timer1
- TI0 = 1; // Indicate TX0 ready
- }
- void Ext_Interrupt_Init (void)
- {
- TCON |= 0x05; // /INT 0 and /INT 1 are edge triggered
- IT01CF = 0x10; // /INT0 active low; /INT0 on P0.0;
- // /INT1 active low; /INT1 on P0.1
- EX0 = 1; // Enable /INT0 interrupts
- EX1 = 1; // Enable /INT1 interrupts
- }
- void INT1_ISR (void) interrupt 2
- {
-
- //同步计时器
- TR0 = 0;
- TL0 = TH0=191;
- TR0 = 1; // START Timer1
- //
- if(tick>=34) tick=0;
- //flagp01=P0^1;
- tickTemp=tick;
- tick=0;
- if(flagR&&(Sent<=9)&&(tickTemp>=11)&&(tickTemp<=28))
- {
- dataR[Sent]=tickTemp-11;
- Sent++;
- }
-
- if(flagR&&(Sent>=8))
- {
- Sent=0;
- flagR=0;
- flag2=1;
- }
- // if((tickTemp<10)||(tickTemp>28))
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
Sent协议转RS232.rar
(40.53 KB, 下载次数: 79)
|