1 | #include <stdio.h> /* standard I/O .h-file */
|
2 | #include <LPC213x.H> /* LPC213x definitions */
|
3 |
|
4 | typedef struct
|
5 | {
|
6 | unsigned long int baudrate;
|
7 | unsigned char dataBits;
|
8 | unsigned char stopBits;
|
9 | unsigned char parity;
|
10 | unsigned char PCD_adress;
|
11 | } COM_TYP;
|
12 |
|
13 | COM_TYP com_var_1;
|
14 | /******************************************************************************/
|
15 | void init_serial (void)
|
16 | {
|
17 | IODIR1 = 0x03000000; /* LEDs P1.24,P1.23 defined as Outputs */
|
18 | PINSEL0 = 0x00050005; /*Enable TxD1,TxD0,RxD1,RxD0*/
|
19 | IODIR0 = 0x10400410; /*OUT: RTS0_P0.4, RTS1_P0.14, COM1_P0.22, COM0_P0.28*/
|
20 | IOCLR0 = 0x10400010; /*LOW auf 0 COM1,0 = 0(RS485), RTS0=0(Sender)*/
|
21 | IOSET0 = 0x00000400; /*HIGH auf 1 RTS1=1(Empfänger),P0.10*/
|
22 | }
|
23 | /******************************************************************************/
|
24 | void init_uart1(void)
|
25 | {
|
26 | U1LCR = 0x83; /*8 bits, 1 stopbit, no Parity, enable DLAB*/
|
27 | U1DLM = 0x01; /*9600 Baud Rate, 55.296.000 PCLK*/
|
28 | U1DLL = 0x68; /*360dez=0x0168*/
|
29 | U1LCR = 0x03; /*disable DLAB*/
|
30 | U1FCR = 0x01; /*enable UART1-FIFO*/
|
31 | }
|
32 | /******************************************************************************/
|
33 | funktion_mit_pcd_werten_fuellen(COM_TYP *pointer)
|
34 | {
|
35 | pointer->baudrate = 9600;
|
36 | pointer->dataBits = 8;
|
37 | pointer->stopBits = 1;
|
38 | pointer->parity = 0;
|
39 | pointer->PCD_adress = 77;
|
40 | }
|
41 | /******************************************************************************/
|
42 | int uart0_init(COM_TYP *pointer)
|
43 | {
|
44 | unsigned char ucTemp;
|
45 |
|
46 | ucTemp = 0;
|
47 | // Word Lenght Select
|
48 | if (dataBits == 8)
|
49 | ucTemp |= 0x03;
|
50 | else if (dataBits == 7)
|
51 | ucTemp |= 0x02;
|
52 | else
|
53 | return(1);
|
54 |
|
55 | // Stop Bit Select
|
56 | if (stopBits == 1 )
|
57 | ucTemp |= 0x00;
|
58 | else if ( stopBits == 2 )
|
59 | ucTemp |= 0x04;
|
60 | else
|
61 | return(1);
|
62 |
|
63 | // Parity Enable
|
64 | if (parity == 0) //disable
|
65 | ucTemp |= 0x00;
|
66 | else if (parity == 1) //enable
|
67 | ucTemp |= 0x04;
|
68 | else
|
69 | return(1);
|
70 |
|
71 | //Parity Select
|
72 | if (parity == 'o')
|
73 | ucTemp |= 0x00;
|
74 | else if (parity == 'e')
|
75 | ucTemp |= 0x10;
|
76 | else if (parity == 1)
|
77 | ucTemp |= 0x20;
|
78 | else if (parity == 0)
|
79 | ucTemp |= 0x30;
|
80 | else
|
81 | return(1);
|
82 |
|
83 | // Devisor Latch Access Bit(DLAB) enable
|
84 | ucTemp |= 0x80;
|
85 | U0LCR = ucTemp; /*8 bits, 1 stopbit, no Parity, enable DLAB*/
|
86 |
|
87 | // Baudrate Select
|
88 | switch ( baudrate )
|
89 | {
|
90 | case 4800:
|
91 | U0DLM = 0x02; /*4800 Baud Rate, 55.296.000 PCLK*/
|
92 | U0DLL = 0xD0; /*720dez=0x02Do*/
|
93 | break;
|
94 | case 9600:
|
95 | U0DLM = 0x01; /*9600 Baud Rate, 55.296.000 PCLK*/
|
96 | U0DLL = 0x68; /*360dez=0x0168*/
|
97 | break;
|
98 | case 19200:
|
99 | U0DLM = 0x00; /*19200 Baud Rate, 55.296.000 PCLK*/
|
100 | U0DLL = 0x64; /*180dez=0x0064*/
|
101 | break;
|
102 | case 38400:
|
103 | U0DLM = 0x00; /*38400 Baud Rate, 55.296.000 PCLK*/
|
104 | U0DLL = 0x5A; /*90dez=0x005A*/
|
105 | break;
|
106 | case 76800:
|
107 | U0DLM = 0x00; /*9600 Baud Rate, 55.296.000 PCLK*/
|
108 | U0DLL = 0x68; /*45dez=0x003D*/
|
109 | break;
|
110 | default:
|
111 | //return(ERR_COM_INIT_LINE_SETTINGS);
|
112 | break;
|
113 |
|
114 | }
|
115 |
|
116 | // Devisor Latch Access Bit(DLAB) disable
|
117 | ucTemp = 0;
|
118 | ucTemp |= 0x00;
|
119 | U0LCR = ucTemp;
|
120 |
|
121 | //FIFO UART enable
|
122 | U0FCR = 0x01;
|
123 | return 1;
|
124 | }
|
125 | /******************************************************************************/
|
126 | void sendchar (void) { /* Write character to Serial Port */
|
127 | while (!(U0LSR & 0x20));
|
128 | (U0THR = 0x89);
|
129 | (U0THR = 0x89);
|
130 | (U0THR = 0x89);
|
131 | (U0THR = 0x89);
|
132 | (U0THR = 0x89);
|
133 | (U0THR = 0x89);
|
134 | (U0THR = 0x89);
|
135 | (U0THR = 0x89);
|
136 | }
|
137 | /******************************************************************************/
|
138 | int receivechar (void) { /* Read character from Serial Port */
|
139 |
|
140 | while (!(U1LSR & 0x01));
|
141 |
|
142 | return (U1RBR);
|
143 | }
|
144 | /******************************************************************************/
|
145 |
|
146 | int main (void)
|
147 | {
|
148 |
|
149 | init_serial(); /* Initialize Serial Interface */
|
150 | funktion_mit_pcd_werten_fuellen(&com_var_1);
|
151 | init_uart1();
|
152 | uart0_init(&com_var_1);
|
153 | do
|
154 | { /* Loop forever */
|
155 | {
|
156 | sendchar ();
|
157 | }
|
158 | } while(1);
|
159 | }
|