Subversion Repositories group.electronics

Rev

Rev 122 | Rev 127 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>

#include "avrutil.h"
#include "e2p.h"
#include "wire.h"
#include "mio.h"
#include "lcd.h"
#include "config.h"
#include "uart.h"

#ifndef NULL
#define NULL    ((void *)0)
#endif


/* ------------------------------------------------------------------------- */

#define UART_BAUD_RATE  9600

volatile uint8_t tmr0_ovf = 0;
volatile uint32_t systime = 0;

#define UART_SEND_DELAY 5000
#define BUTTON_DELAY 500
#define LCD_SEND_DELAY 80

volatile uint16_t buttontime = BUTTON_DELAY;
volatile uint16_t sendtime = UART_SEND_DELAY;
volatile uint8_t lcdTimer = LCD_SEND_DELAY;


int main(void) {


  /*
  DDR : 1 = Output, 0 = Input
  PORT: 1 = Pullup for Input, otherwise set output
  PIN : Read input pin
  */

  /*
        PB0     - 
        PB1     - 
        PB2     - 
        PB3     - 
        PB4     - 
        PB5     - 
        PB6     - 
        PB7     - 
  */
  DDRB          = 0B00001111;
  PORTB         = 0B00000000;

  /*
        PC0     - 
        PC1     - 
        PC2     - 
        PC3     - 
        PC4     - 
        PC5     - 
  */
  DDRC          = 0B11111111;
  PORTC         = 0B00000000;

  /*
        PD0     - 
        PD1     - 
        PD2     - 
        PD3     - 
        PD4     - 
        PD5     - Input PcInt           - * I/O Interrupt PCINT21
        PD6     - Output                - * Status LED
        PD7     - Input, Pullup         - * Button
  */
  DDRD          = 0B01011111;
  PORTD         = 0B10000000;

 
  
    sysclockInit();   
    
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
  
    wdt_enable(WDTO_1S);                // Watchdog for 1 sec
    sei();                              // Enable interrupts
    
    i2c_master();
    e2p_init();
    mio_init();
    lcd_init();
   
    lcd_setCursor(0, 1);
    lcd_print("Time:\0");
    
    char display[10];
  
    uint16_t address = 0x0003;

    sbi(PORTD, PD6); 
    _delay_us(100);
    cbi(PORTD, PD6);
    
    mio_iodir(0x02);
    mio_pullups(0x02);
    //mio_writeReg(0x00, 0x02);
    //mio_writeReg(0x06, 0x02);
    //mio_writeReg(0x0a, 0x01);
    //mio_writeReg(0x0a, 0x00);
   

    for(;;){
        wdt_reset();
        cbi(PIND, PD6);
        
        if (!rbi(PIND, PD7) && !buttontime) {
          
          sbi(PORTD, PD6);
          delay_ms(1);
          cbi(PORTD, PD6);
          
          uint8_t lsb = systime & 0x000000ff;
          
          e2p_writeByte(address, lsb);
          e2p_flush();    
          uint8_t rdata = e2p_readByte(address);
          
          uart_puts("Write: ");
          utoa(lsb, display, 16);
          uart_puts(display);
          uart_puts("\r\n"); 
          lcd_overprint_right(display, 2, 3, 0);
          
          uart_puts("Read: ");
          utoa(rdata, display, 16);
          uart_puts(display);
          uart_puts("\r\n");
          lcd_overprint_right(display, 2, 6, 0);
          buttontime = BUTTON_DELAY;
        }
           
        
        if (!sendtime) {
          ltoa(systime, display, 10);
          uart_puts(display);
          uart_puts("\r\n");
          
          sendtime=UART_SEND_DELAY;
          

        }
         
        if (!lcdTimer) {
          ultoa(systime, display, 10);
          lcd_overprint_right(display, 10, 6, 1);

          uint8_t but1 = mio_readPin(1);
          if (but1) {
            cbi(PORTD, PD6);
            mio_latchPin(0, 0x00);
          } else {
            sbi(PORTD, PD6);
            mio_latchPin(0, 0x01);
          }
          
          utoa(but1, display, 16);
          lcd_overprint_right(display, 2, 0, 0);
          
          lcdTimer = LCD_SEND_DELAY;
        }
        _delay_ms(1);
        
    }
    
    return 0;
}


ISR(TIMER0_OVF_vect) {
        tmr0_ovf++;

        // Clk/1 TCCR0B = (1<< CS00);
        //20.0Mhz, 1ms = 78ovf
        //16.5Mhz, 1ms = 64ovf
        //16.0Mhz, 1ms = 62ovf
        //12.0Mhz, 1ms = 46ovf

        if (tmr0_ovf>=64) {
                systime++;
                tmr0_ovf = 0;
                if (sendtime)
                  sendtime--;
                
                if (lcdTimer)
                    lcdTimer--;
                
                if (buttontime)
                    buttontime--;
        }
}