原创
在KEIL平台基础上实现LPC2132与MAX485进行RS485通信
- http://www.pudn.com/downloads126/sourcecode/embed/detail533096.html
- #include <LPC213X.H>
-
- typedef unsigned char uint8;
- typedef signed char int8;
- typedef unsigned short uint16;
- typedef signed short int16;
- typedef unsigned int uint32;
- typedef signed int int32;
- typedef float fp32;
- typedef double fp64;
-
- #define Fosc 12000000 //Crystal frequence,10MHz~25MHz,should be the same as actual status.
-
- #define Fcclk (Fosc * 4) //System frequence,should be (1~32)multiples of Fosc,and should be equal or less than 60MHz.
-
- #define Fcco (Fcclk * 4) //CCO frequence,should be 2、4、8、16 multiples of Fcclk, ranged from 156MHz to 320MHz.
-
- #define Fpclk (Fcclk / 4) * 1 //VPB clock frequence , must be 1、2、4 multiples of (Fcclk / 4).
-
-
-
-
-
-
-
-
-
-
-
- void DelayNS (uint32 dly)
- {
- uint32 i;
-
- for ( ; dly>0; dly--)
- for (i=0; i<50000; i++);
- }
- #define UART_BPS 115200 // 串口通讯波特率
-
-
-
-
-
-
-
-
- void UART0_Init (void)
- {
- uint16 Fdiv;
-
- U0LCR = 0x83;
- Fdiv = (Fpclk / 16) / UART_BPS;
- U0DLM = Fdiv / 256;
- U0DLL = Fdiv % 256;
- U0LCR = 0x03;
- }
-
-
-
-
-
-
-
-
- uint8 UART0_GetByte (void)
- {
- uint8 rcv_dat;
- IO0CLR |=1<<31;
- while ((U0LSR & 0x01) == 0);
- rcv_dat = U0RBR;
-
- return (rcv_dat);
- }
-
-
-
-
-
-
-
-
-
- void UART0_GetStr (uint8 *s, uint32 n)
- {
- for ( ; n>0; n--)
- {
- *s++ = UART0_GetByte();
- }
- }
-
-
-
-
-
-
-
-
- void UART0_SendByte (uint8 dat)
- {
- IO0SET |=1<<31;
- U0THR = dat;
- while ((U0LSR & 0x40) == 0);
- }
-
-
-
-
-
-
-
-
- void UART0_SendStr (uint8 const *str)
- {
- while (1)
- {
- if (*str == '\0') break;
- UART0_SendByte(*str++);
- }
- }
-
-
-
-
-
-
-
-
- int main (void)
- {
- uint8 snd[2];
- uint8 RCV[2];
- memset(RCV,0,2);
- IO0DIR |=1<<31;
- IO0CLR |=1<<31;
- PINSEL0 |=0x05;
-
-
- UART0_Init();
- snd[0]='a';
- snd[1]='b';
-
-
- while (1)
- {
-
- DelayNS(10);
- UART0_GetStr(RCV,2);
- DelayNS(10);
- }
-
- return 0;
- }
-
-
-
-
-
-
-
-
关闭
站长推荐
/3
文章评论(0条评论)
登录后参与讨论