/[H9]/trunk/Embedded/main.c
ViewVC logotype

Annotation of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 137 - (hide annotations) (download)
Mon Dec 3 14:24:50 2007 UTC (16 years, 6 months ago) by hedin
File MIME type: text/plain
File size: 3049 byte(s)
WIP commit of embedded, about to init sms modem etc.
1 hedin 82 #include <pic18.h>
2     #include <stdio.h>
3     #include <string.h>
4     #include "lcd.h"
5 hedin 137 #include "Delay.h"
6 hedin 109 // Delay.h is included inside lcd.c
7 hedin 82
8 hedin 109 #define LCD_LENGTH 16
9     #define LCD_ROWS 2
10    
11    
12 hedin 137 unsigned char global_Pot_Hi, global_Pot_Lo;
13 hedin 109 unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];
14 hedin 137 unsigned char global_serial_data;
15     unsigned char global_serial_recieve_buffer[LCD_LENGTH];
16     bit global_recieve_done = 0;
17     int global_serial_byte_counter = 0;
18    
19     void serial_recieved(void);
20    
21 hedin 109 // Nicked from H7
22     void ad_init(void)
23     {
24     // AD Conversion clock
25     ADCS0 = 0;
26     ADCS1 = 0;
27     ADCS2 = 0;
28 hedin 82
29 hedin 109 //Select AN0/RA0 for AD source
30     // In this (000) setup, it's only RA0/AN0 that does ad convertion.
31     CHS0=0;
32     CHS1=0;
33     CHS2=0;
34    
35     //Only AN0 is selected for AD and with Vdd/Vss as limits
36     PCFG0=0;
37     PCFG1=1;
38     PCFG2=1;
39     PCFG3=1;
40    
41 hedin 112 //Result is left justified
42     ADFM=0;
43 hedin 109
44     //Fire up for A/D converter module
45     ADON=1;
46     }
47    
48 hedin 82 void rs232_init(void)
49     {
50     SPEN = 0; // Serial Port Enable Bit... 0 = disabled
51 hedin 137 TRISC6 = 0;
52 hedin 82 TRISC7 = 1;
53 hedin 137
54     SPBRG = 207; // 1200 baud rate... 25 = 9600
55     // x = (Fosc / (16*[baud rate]) )-1
56     TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register
57     RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
58 hedin 82 }
59    
60     void interrupt_init(void)
61     {
62     // Assumes that all interrupts default is 0
63     PEIE = 1;
64     GIE = 1;
65 hedin 137 RCIE = 1; // Recieve interrupt enable.
66     IPEN = 0; // Nfo interrupt priority
67     TXIE = 0; // Serial interrupt enabled
68 hedin 82 }
69    
70     void pic18_io_init(void)
71     {
72 hedin 109 TRISA0 = 1; // analog input
73     TRISB1 = 1; // TRISB1-4 Digital input
74 hedin 82 TRISB2 = 1;
75     TRISB3 = 1;
76     TRISB4 = 1;
77     }
78    
79 hedin 137 void sms_init(void)
80 hedin 82 {
81     }
82    
83     void interrupt interrupt_handler(void)
84     {
85 hedin 109 // Finds out what interrupt have been trigged, and starts the respective function.
86 hedin 137 if(RCIF == 1) // Serial recieve interrupt
87 hedin 82 {
88 hedin 137 serial_recieved();
89 hedin 82 RCIF = 0;
90     }
91     }
92 hedin 109
93 hedin 137
94    
95     void serial_send(void)
96     {
97     int i;
98     char tosend[3];
99     char data;
100     sprintf(tosend,"%s", "at\r");
101     for(i = 0; i < 3; i++)
102 hedin 109 {
103 hedin 137 data = tosend[i];
104     TXREG = data;
105     while(TRMT==0)
106     DelayMs(1000);
107 hedin 109 }
108 hedin 137 }
109    
110     void serial_recieved(void)
111     {
112     char data, saved_data[LCD_LENGTH];
113    
114     data = RCREG;
115    
116     global_serial_recieve_buffer[global_serial_byte_counter] = data;
117     if(data == '\r')
118     {
119     global_recieve_done = 1;
120     global_serial_byte_counter = 0;
121     }
122 hedin 109 else
123     {
124 hedin 137 global_serial_byte_counter++;
125 hedin 109 }
126    
127 hedin 137 }
128 hedin 109
129 hedin 82 void main()
130     {
131 hedin 109 /////////////////////////////////////////////
132     // Running Init's
133    
134     // Running init for various components.
135     //AD_init();
136     ad_init();
137 hedin 82 rs232_init();
138     pic18_io_init();
139 hedin 109 lcd_init(0);
140 hedin 137 interrupt_init();
141    
142 hedin 109 /////////////////////////////////////////////
143     // Main loop
144    
145     while(1)
146 hedin 100 {
147 hedin 109 // Checking if A/D convertion is done, and save the data in global_Pot_??
148     if(GODONE==0)
149     {
150     global_Pot_Hi = ADRESH;
151     global_Pot_Lo = ADRESL;
152 hedin 112 GODONE = 1;
153 hedin 109 }
154 hedin 100
155 hedin 137 if( global_recieve_done == 1 )
156     {
157     lcd_clear();
158     lcd_goto(0x00);
159     lcd_puts(global_serial_recieve_buffer);
160     global_recieve_done = 0;
161     }
162 hedin 100 }
163 hedin 82 }

  ViewVC Help
Powered by ViewVC 1.1.20