/[H7]/trunk/PIC/main.c
ViewVC logotype

Diff of /trunk/PIC/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 20 by torben, Tue Jan 30 22:16:53 2007 UTC revision 26 by torben, Wed Jan 31 10:45:20 2007 UTC
# Line 67  bit global_comm_ready; Line 67  bit global_comm_ready;
67  bit global_comm_error;  bit global_comm_error;
68    
69  unsigned char global_comm_slipstate;  unsigned char global_comm_slipstate;
70  unsigned char global_comm_currentrate;  unsigned char global_comm_baudrate;
71    
72  unsigned char global_lcd_buffer[20];  unsigned char global_lcd_buffer[2][20];
 unsigned char global_lcd_index;  
73    
74    
75  bit global_led_0;  bit global_led_0;
# Line 89  unsigned char global_potmeter_lo; Line 88  unsigned char global_potmeter_lo;
88  void recieve_interrupt(void)  void recieve_interrupt(void)
89  {  {
90          unsigned char data = RCREG;              unsigned char data = RCREG;    
91          RB2 = !RB2;          
92            if (global_comm_length == BUFFERLEN) //avoid buffer overrun
93                    global_comm_slipstate == SlipError;
94                    
95          switch (global_comm_slipstate)          switch (global_comm_slipstate)
96          {                        {              
# Line 140  void transmit_interrupt(void) Line 141  void transmit_interrupt(void)
141          RB2 = !RB2;          RB2 = !RB2;
142  }  }
143  */  */
144    //Timer1 er en 16 bit timer, der kører med en 1:8 prescaler,
145    // og er styret fra en ekstern 32768 Hz krystal
146    //Når at registrene preloades med 0xEFFF vil det resultere i en
147    //timer overflow ca hvert sekund
148    void timer1_interrupt(void)
149    {
150            TMR1H = 0xEF;
151            TMR1L = 0xFF;
152    }
153  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
154  {  {
155          if (RCIF == 1)          if (RCIF == 1)
# Line 154  void interrupt interrupt_handler(void) Line 163  void interrupt interrupt_handler(void)
163                  TXIF = 0;                  TXIF = 0;
164          }          }
165          */          */
166            
167            if (TMR1IF == 1)
168            {
169                    timer1_interrupt();
170                    TMR1IF = 0;
171            }
172  }  }
173    
174  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
175  // Slip funktioner  // Slip funktioner
176    
177    
178  void slip_reset(void)  void slip_reset(void)
179  {  {
180          global_comm_error = 0;          global_comm_error = 0;
# Line 239  void slip_highlevel_protocol(void) Line 255  void slip_highlevel_protocol(void)
255                                  {                                  {
256                                          //we can only handle 1200,2400,9600,19200                                          //we can only handle 1200,2400,9600,19200
257                                          if (data == 0 || data == 1 || data == 3 || data == 4)                                          if (data == 0 || data == 1 || data == 3 || data == 4)
258                                            {
259                                                  global_reset_baudrate = 1;                                                  global_reset_baudrate = 1;
260                                                    global_comm_baudrate = data;
261                                            }
262                                          else                                          else
263                                                  global_comm_error = 1;                                                  global_comm_error = 1;
264                                  }                                  }
# Line 298  void slip_send_response(void) Line 317  void slip_send_response(void)
317  }  }
318    
319  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
320  // Gennerelle funktioner  // init funktioner
321    
322  void interrupt_init(void)  void interrupt_init(void)
323  {  {
# Line 311  void interrupt_init(void) Line 330  void interrupt_init(void)
330                                    
331          TXIF = 0; //nulstil intterupt flag          TXIF = 0; //nulstil intterupt flag
332          RCIF = 0;          RCIF = 0;
333            
334            TMR1IE = 1; // Timer 1
335    
336          GIE = 1; //Global interrupt enable bit          GIE = 1; //Global interrupt enable bit
337  }  }
# Line 402  void i2c_init(void) Line 423  void i2c_init(void)
423  #endif  #endif
424  }  }
425    
426    void timer_init(void)
427    {
428            TMR1CS = 1; //use external clock
429            
430            T1CKPS1 = 1; //1:8 prescale
431            T1CKPS0 = 1;
432            
433            TMR1H = 0xEF;
434            TMR1L = 0xFF;
435    
436            T1OSCEN = 1; //enable oscillator circuit        
437            RD16 = 0; //normal 8 bit writes
438            TMR1ON = 1;
439    }
440    
441    ///////////////////////////////////////////////////////////////////
442    // Gennerelle funktioner
443    
444    
445  char ReadTemp(void)  char ReadTemp(void)
# Line 415  char ReadTemp(void) Line 453  char ReadTemp(void)
453          return temp;          return temp;
454  }  }
455    
456    void update_lcd(void)
457    {
458    }
459    
460    void reset_baudrate(void)
461    {
462            SPEN = 0; //disable serial port
463            
464            //set baudrate generator, i henhold til side 171 af PIC18F452 dokumentationen
465            switch (global_comm_baudrate)
466            {
467                    case Baud1200:
468                            SPBRG = 207;
469                            break;
470                    case Baud2400:
471                            SPBRG = 103;
472                            break;
473                    case Baud9600:
474                            SPBRG = 25;
475                            break;
476                    case Baud19200:
477                            SPBRG = 12;
478                            break;
479                    default:
480                            SPBRG = 25; // this should not be possible, but default to 9600 anyway
481            }
482            SPEN = 1; // enable the serial port again
483    }
484    
485  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
486  //Main  //Main
# Line 427  void main(void) Line 493  void main(void)
493          serial_init(); //9600 8N1          serial_init(); //9600 8N1
494          ad_init();          ad_init();
495          io_init();          io_init();
496            timer_init();
497          interrupt_init();          interrupt_init();
498                    
499    
500          slip_reset(); //take SLIP FSM into normal state          slip_reset(); //take SLIP FSM into normal state
501          global_comm_currentrate = Baud9600; //default baudrate = 9600          global_comm_baudrate = Baud9600; //default baudrate = 9600 (allready set in serial_init)
502          global_reset_baudrate = 0;          global_reset_baudrate = 0;
503                    
504  //      initialisation completed and we are ready to recieve commands - enable serial reception  //      initialisation completed and we are ready to recieve commands - enable serial reception
# Line 448  void main(void) Line 515  void main(void)
515                                    
516                  if ( global_reset_baudrate == 1)                  if ( global_reset_baudrate == 1)
517                  {                  {
518                            reset_baudrate();
519                  }                  }
520                                    
521                  RB1 = global_led_0;                  RB1 = global_led_0;

Legend:
Removed from v.20  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.20