Projects

Atmega8535
iBot
Light_controller
    preAmpli
    This device allows to test a LDC graphic screen of 128X64 pixels with an Atmel microncontroller.

    Connections

    Source code

    /*************************************************
    * Description     : LCD graphic 128X64 Test
    * Last update     : 18 November 2005
    * Crystal     : 8 Or 16 MHz
    * Email          : geoffroy@lumay.be
    * OK for avr-libc 1.2 ! (tested)
    *************************************************/


    #include <avr/io.h>
    #include <avr/io8535.h>

    #define CRYSTAL 16000000                     // MHz
                                                                //#define CRYSTAL 8000000
    #define LCD_COM_PORT PORTB // LCD port
    #define LCD_DATA_PORT      PORTC // LCD data port
    #define LCD_DATA_PORT_IN PINC //input data LCD
    #define     LCD_E          0 // LCD enable line
    #define     LCD_RW          1 // LCD read/write selecting line
    #define     LCD_DI          2 // LCD Data/instruction selecting line
    #define     LCD_CS1      3 // LCD left/right selecting line
    #define     LCD_CS2      4 // LCD left/right selecting line

    //We redefine some deprecated functions
    #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
    #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

    void init(void){
         DDRB=0xFF; // set LCD_COM_PORT as output
         DDRC=0xFF; // set LCD_DATA_PORT as output
         LCD_COM_PORT=0x00; // reset port
         LCD_DATA_PORT=0x00; // reset port
    }

    // 10 microsec delay
    void delay_10us(void){
         unsigned char test = 1;
         TCCR0=0x02;
         TCNT0=0xEB; //for 16 MHz
                        //TCNT0=0xF5; //for 8 MHz
         while (test){
              test = ~TIFR & 0x01;
    }
         TIFR=0x01;
    }

    //10 millisec delay
    void delay_10ms(void)                          {
         unsigned char test = 1;
         TCCR0=0x05;
         TCNT0=0x63; //for 16 MHz
                        //TCNT0=0xB1; //for 8 MHz

         while (test){
              test = ~TIFR & 0x01;
    }
         TIFR=0x01;
    }

    // Write data on LCD
    void LCDwriteData(unsigned char data){
         sbi(LCD_COM_PORT,LCD_DI);
         cbi(LCD_COM_PORT,LCD_RW);
         LCD_DATA_PORT=data;
         sbi(LCD_COM_PORT,LCD_E);
         delay_10us();
         cbi(LCD_COM_PORT,LCD_E);
    }

    // Read data on LCD
    unsigned char LCDreadData(void){
         unsigned char data;
         LCD_DATA_PORT=0x00;
         DDRC=0x00;
         sbi(LCD_COM_PORT,LCD_RW);
         sbi(LCD_COM_PORT,LCD_DI);
         sbi(LCD_COM_PORT,LCD_E);
         delay_10us();
         data = LCD_DATA_PORT_IN;
         cbi(LCD_COM_PORT,LCD_E);
         DDRC=0xFF;
         return data;
    }

    // write an instruction on LCD
    void LCDwriteInstruction(unsigned char instruction){
         cbi(LCD_COM_PORT,LCD_DI);
         cbi(LCD_COM_PORT,LCD_RW);
         LCD_DATA_PORT=instruction;
         sbi(LCD_COM_PORT,LCD_E);
         delay_10us();
         cbi(LCD_COM_PORT,LCD_E);
    }

    // select left part of the LCD
    void LCDleft(void){
         sbi(LCD_COM_PORT,LCD_CS1);
         cbi(LCD_COM_PORT,LCD_CS2);
         delay_10us();
    }

    // select right part of the LCD
    void LCDright(void){
         cbi(LCD_COM_PORT,LCD_CS1);
         sbi(LCD_COM_PORT,LCD_CS2);
         delay_10us();
    }

    // clear the LDC
    void LCDClear(void){
         unsigned char pages,i;
         for (pages=0;pages<8;pages++){
              LCDright();
              LCDwriteInstruction(64);
              LCDwriteInstruction(184+pages);
              for (i=0;i<64;i++){
                   LCDwriteData(0);
              }
              LCDleft();
              LCDwriteInstruction(64);
              LCDwriteInstruction(184+pages);
              for (i=0;i<64;i++){
                   LCDwriteData(0);
              }
         }
    }

    //initialize the LCD
    void LCDInit(void){
         LCDright();
         LCDwriteInstruction(64);
         LCDwriteInstruction(63);
         LCDwriteInstruction(192);
         LCDleft();
         LCDwriteInstruction(64);
         LCDwriteInstruction(63);
         LCDwriteInstruction(192);
    }

    // set a pixel on LCD
    void LCDsetDot(unsigned char x, unsigned char y){
         unsigned char page;
         unsigned char oldValue;
         if(x<64){
              LCDleft();
         }else{
              LCDright();
              x = x - 64;
         }
         page = 184 + y/8;
         LCDwriteInstruction(page);                // select Page
         LCDwriteInstruction(64+x);                // select Y
         oldValue = LCDreadData();           // read old value
         oldValue = LCDreadData();
         LCDwriteInstruction(page);                // select Page
         LCDwriteInstruction(64+x);                // select Y
         LCDwriteData(oldValue | (1 << (y % 8)));
    }

    // Clear a pixel on LCD
    void LCDclearDot(unsigned char x, unsigned char y){
         unsigned char page;
         unsigned char oldValue;
         if(x<64){
              LCDleft();
         }else{
              LCDright();
              x = x - 64;
         }
         page = 184 + y/8;
         LCDwriteInstruction(page); //select Page
         LCDwriteInstruction(64+x); //select Y
         oldValue = LCDreadData();
         oldValue = LCDreadData();
         LCDwriteInstruction(page); //select Page
         LCDwriteInstruction(64+x); //select Y
         LCDwriteData(oldValue & ~(1 << (y % 8)));
    }

    // Draw a line on LCD
    void LCDsetLine(unsigned char x1, unsigned char y1,unsigned char x2, unsigned char y2){
         unsigned char tamponX,tamponY,x,y;
         if (x2<x1){
              tamponX = x1;
              tamponY = y1;
              x1 = x2;
              y1 = y2;
              x2 = tamponX;
              y2 = tamponY;
         }
         if(x1==x2){
              for(y=y1;y<=y2;y++){
                   LCDsetDot(x1,y);
              }
         }else{
              for(x=x1;x<=x2;x++){
                   if(y1>=y2){
                        y = y1 - (x-x1) * (y1-y2)/(x2-x1);
                   }else{
                        y = y1 + (x-x1) * (y2-y1)/(x2-x1);
                   }
                   LCDsetDot(x,y);
              }
         }
    }

    int main (void){
         init();
         LCDInit();
         LCDClear();
         //Draw diagonal
         LCDsetLine(1,1,127,63);
         LCDsetLine(1,63,127,1);
         //Draw rectangle
         LCDsetLine(1,1,127,1);
         LCDsetLine(1,1,1,63);
         LCDsetLine(127,1,127,63);
         LCDsetLine(1,63,127,63);
         while (1){
         }
    }

    MakeFile

    #Makefile writen by Geoffroy Lumay
    #Path for avr-gcc
    AVRPATH=/usr/local/atmel/bin
    MCU=at90s8535
    CC=$(AVRPATH)/avr-gcc
    OBJCOPY=$(AVRPATH)/avr-objcopy
    # optimization
    CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
    #*****************************************************
    all: ProgAtmel.hex
    ProgAtmel.hex : ProgAtmel.out
    $(OBJCOPY) -R .eeprom -O ihex ProgAtmel.out ProgAtmel.hex
    ProgAtmel.out : ProgAtmel.o
    $(CC) $(CFLAGS) -o ProgAtmel.out -Wl,-Map,ProgAtmel.map ProgAtmel.o
    ProgAtmel.o : ProgAtmel.c
    $(CC) $(CFLAGS) -Os -c ProgAtmel.c
    #******************************************************
    load: ProgAtmel.hex
    # erase before loading the program.
    uisp -dlpt=/dev/parport0 --erase -dprog=dapa
    # load (program) the software into the eeprom:
    uisp -dlpt=/dev/parport0 --upload if=ProgAtmel.hex -dprog=dapa -v=3 --hash=32
    #*******************************************************
    clean:
    rm -f *.o *.map *.out
    #*******************************************************
    fuse:
    #fusing the atmega8535 for quartz
    uisp -dlpt=/dev/parport0 -dprog=dapa --wr_fuse_e=0xFF --wr_fuse_l=0xEF --wr_fuse_h=0xC9
    #********************************************************
    readfuse:
    #Read the fuse
    uisp -dlpt=/dev/parport0 -dprog=dapa --rd_fuses


    To compile the project, type make
    To load the hex file in the controler, type (as su) #make load
    To read the fuses, type (as su) #make readfuse
    To change the fuses of the Atmega8535, type (as su) #make fuse