1 |
1: #include <pic18.h> |
1: #include <pic18.h> |
2 |
2: #include <stdio.h> |
2: #include <stdio.h> |
3 |
3: #include <string.h> |
3: #include <string.h> |
4 |
4: |
4: #include "lcd.h" |
5 |
5: #include "lcd.h" |
5: // Delay.h is included inside lcd.c |
6 |
6: |
6: |
7 |
7: |
7: #define LCD_LENGTH 16 |
8 |
8: void rs232_init(void) |
8: #define LCD_ROWS 2 |
9 |
9: { |
9: |
10 |
10: SPEN = 0; // Serial Port Enable Bit... 0 = disabled |
10: |
11 |
11: TRISC6 = 0; |
11: unsigned char global_Pot_Hi; |
12 |
12: TRISC7 = 1; |
12: unsigned char global_Pot_Lo; |
13 |
13: SYNC = 0; // SYNC switches between async(0) and sync(1) mode. |
13: unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; |
14 |
14: SPBRG = 25; |
14: /*void AD_init(void) |
15 |
15: TXSTA = 0x24; |
15: { |
16 |
16: RCSTA = 0x90; |
16: ADON = 1; |
17 |
17: SPEN = 1; |
17: } |
18 |
18: } |
18: */ |
19 |
19: |
19: // Nicked from H7 |
20 |
20: void interrupt_init(void) |
20: void ad_init(void) |
21 |
21: { |
21: { |
22 |
22: // Assumes that all interrupts default is 0 |
22: // AD Conversion clock |
23 |
23: PEIE = 1; |
23: ADCS0 = 0; |
24 |
24: GIE = 1; |
24: ADCS1 = 0; |
25 |
25: RCIE = 1; |
25: ADCS2 = 0; |
26 |
26: } |
26: |
27 |
27: |
27: //Select AN0/RA0 for AD source |
28 |
28: void pic18_io_init(void) |
28: // In this (000) setup, it's only RA0/AN0 that does ad convertion. |
29 |
29: { |
29: CHS0=0; |
30 |
30: TRISA0 = 1; |
30: CHS1=0; |
31 |
31: TRISB1 = 1; |
31: CHS2=0; |
32 |
32: TRISB2 = 1; |
32: |
33 |
33: TRISB3 = 1; |
33: //Only AN0 is selected for AD and with Vdd/Vss as limits |
34 |
34: TRISB4 = 1; |
34: PCFG0=0; |
35 |
35: } |
35: PCFG1=1; |
36 |
36: |
36: PCFG2=1; |
37 |
37: void interrupt_recieve_handler(void) |
37: PCFG3=1; |
38 |
38: { |
38: |
39 |
39: // Handle recieve inputs... |
39: //Result is left justified |
40 |
40: } |
40: ADFM=0; |
41 |
41: |
41: |
42 |
42: void interrupt interrupt_handler(void) |
42: //Fire up for A/D converter module |
43 |
43: { |
43: ADON=1; |
44 |
44: if(RCIF == 1) |
44: } |
45 |
45: { |
45: |
46 |
46: interrupt_recieve_handler(); |
46: void rs232_init(void) |
47 |
47: RCIF = 0; |
47: { |
48 |
48: } |
48: SPEN = 0; // Serial Port Enable Bit... 0 = disabled |
49 |
49: } |
49: TRISC6 = 0; |
50 |
50: |
50: TRISC7 = 1; |
51 |
51: |
51: SYNC = 0; // SYNC switches between async(0) and sync(1) mode. |
52 |
52: void main() |
52: SPBRG = 25; |
53 |
53: { |
53: TXSTA = 0x24; |
54 |
54: rs232_init(); |
54: RCSTA = 0x90; |
55 |
55: pic18_io_init(); |
55: SPEN = 1; |
56 |
56: } |
56: } |
57 |
|
57: |
58 |
|
58: void interrupt_init(void) |
59 |
|
59: { |
60 |
|
60: // Assumes that all interrupts default is 0 |
61 |
|
61: PEIE = 1; |
62 |
|
62: GIE = 1; |
63 |
|
63: RCIE = 1; |
64 |
|
64: } |
65 |
|
65: |
66 |
|
66: void pic18_io_init(void) |
67 |
|
67: { |
68 |
|
68: TRISA0 = 1; // analog input |
69 |
|
69: TRISB1 = 1; // TRISB1-4 Digital input |
70 |
|
70: TRISB2 = 1; |
71 |
|
71: TRISB3 = 1; |
72 |
|
72: TRISB4 = 1; |
73 |
|
73: } |
74 |
|
74: |
75 |
|
75: void interrupt_recieve_handler(void) |
76 |
|
76: { |
77 |
|
77: // Handle recieve inputs... |
78 |
|
78: } |
79 |
|
79: |
80 |
|
80: void interrupt interrupt_handler(void) |
81 |
|
81: { |
82 |
|
82: // Finds out what interrupt have been trigged, and starts the respective function. |
83 |
|
83: if(RCIF == 1) |
84 |
|
84: { |
85 |
|
85: interrupt_recieve_handler(); |
86 |
|
86: RCIF = 0; |
87 |
|
87: } |
88 |
|
88: } |
89 |
|
89: |
90 |
|
90: void update_lcd(void) |
91 |
|
91: { |
92 |
|
92: char toLCD0[LCD_LENGTH], toLCD1[LCD_LENGTH]; |
93 |
|
93: char B1[LCD_LENGTH]; |
94 |
|
94: char sprintF[LCD_LENGTH]; |
95 |
|
95: lcd_goto(0x00); |
96 |
|
96: sprintf(toLCD0, "%s", "Ejdesgaard"); |
97 |
|
97: if(RB1) |
98 |
|
98: { |
99 |
|
99: sprintf(B1, "Pwr%s", "True"); |
100 |
|
100: } |
101 |
|
101: else |
102 |
|
102: { |
103 |
|
103: sprintf(B1, "%s", "False"); |
104 |
|
104: } |
105 |
|
105: lcd_puts(B1); |
106 |
|
106: |
107 |
|
107: lcd_goto(0x40); |
108 |
|
108: sprintf(sprintF,"%08d%08d", global_Pot_Hi, global_Pot_Lo); |
109 |
|
109: lcd_puts(sprintF); |
110 |
|
110: |
111 |
|
111: } |
112 |
|
112: |
113 |
|
113: void main() |
114 |
|
114: { |
115 |
|
115: ///////////////////////////////////////////// |
116 |
|
116: // Running Init's |
117 |
|
117: |
118 |
|
118: // Running init for various components. |
119 |
|
119: //AD_init(); |
120 |
|
120: ad_init(); |
121 |
|
121: rs232_init(); |
122 |
|
122: pic18_io_init(); |
123 |
|
123: lcd_init(0); |
124 |
|
124: |
125 |
|
125: ///////////////////////////////////////////// |
126 |
|
126: // Main loop |
127 |
|
127: |
128 |
|
128: while(1) |
129 |
|
129: { |
130 |
|
130: // Checking if A/D convertion is done, and save the data in global_Pot_?? |
131 |
|
131: if(GODONE==0) |
132 |
|
132: { |
133 |
|
133: global_Pot_Hi = ADRESH; |
134 |
|
134: global_Pot_Lo = ADRESL; |
135 |
|
135: GODONE = 1; |
136 |
|
136: } |
137 |
|
137: |
138 |
|
138: update_lcd(); |
139 |
|
139: } |
140 |
|
140: } |