Facebook
From Krzysztof Karolak, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 108
  1. #define F_CPU 16000000L
  2. #include <avr/io.h>
  3. #include<util/delay.h>
  4. #define EEDEVADR 0b10100000
  5. #define ERROR 1
  6. #define SUCCESS (!ERROR)
  7.  
  8. #define D7 7
  9. #define D6 6
  10. #define D5 5
  11. #define D4 4
  12. #define E 3
  13. #define RS 2
  14. #define RW 1
  15. #define SS 2
  16. #define MOSI 3
  17. #define MISO 4
  18. #define SCK 5
  19.  
  20. void lcd_4bity(unsigned char fourbits)
  21. {
  22.   PORTD |= (1<<E);
  23.   if( fourbits & 0x01) PORTD |= (1<<D4); else PORTD &= ~(1<<D4);
  24.   if( fourbits & 0x02) PORTD |= (1<<D5); else PORTD &= ~(1<<D5);
  25.   if( fourbits & 0x04) PORTD |= (1<<D6); else PORTD &= ~(1<<D6);
  26.   if( fourbits & 0x08) PORTD |= (1<<D7); else PORTD &= ~(1<<D7);
  27.   PORTD &= ~(1<<E);
  28.  
  29. }
  30.  
  31. void lcd_bajt(unsigned char bajt)
  32. {
  33.   PORTD |= (1<<E);
  34.   lcd_4bity(bajt>>4);
  35.   PORTD &= ~(1<<E);
  36.   _delay_us(5);
  37.   PORTD |= (1<<E);
  38.   lcd_4bity(bajt);
  39.   PORTD &= ~(1<<E);
  40.   _delay_ms(1);
  41.  
  42. }
  43.  
  44. void ini_lcd(void)
  45. {
  46.   lcd_4bity(3);
  47.   _delay_ms(10);
  48.   lcd_4bity(3);
  49.   _delay_ms(1);
  50.   lcd_4bity(3);
  51.   _delay_ms(1);
  52.   lcd_4bity(2);
  53.   _delay_ms(1);
  54.  
  55.   lcd_bajt(28);
  56.   _delay_ms(1);
  57.   lcd_bajt(20);
  58.   _delay_ms(1);
  59.   lcd_bajt(12);
  60.   _delay_ms(1);
  61.   lcd_bajt(6);
  62.   _delay_ms(1);
  63.   lcd_bajt(1);
  64.   _delay_ms(10);
  65.   lcd_bajt(128);
  66.   _delay_ms(5);
  67. }
  68.  
  69. char aData;
  70. char bData;
  71.  
  72. void SPI_Init(void)
  73. {
  74.   DDRB |= (1<<SS);
  75.   PORTB &= ~(1<<SS);
  76.   /* Set MOSI and SCK output, all others input */
  77.   DDRB |= (1<<MOSI)|(1<<SCK);
  78.   PORTB |= (1<<MISO);
  79.   /* Enable SPI, Master, set clock rate fck/16 */
  80.   SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPHA);
  81.  
  82. }
  83.  
  84. char SPI_Transmit(char aData, char bData)
  85. {
  86.   PORTB |= (1<<SS);
  87.   /* Start transmission */
  88.   SPDR = aData;
  89.   /* Wait for transmission complete */
  90.   while(!(SPSR & (1<<SPIF)));
  91.   SPDR = bData;
  92.   /* Wait for transmission complete */
  93.   while(!(SPSR & (1<<SPIF)));
  94.   PORTB &= ~(1<<SS);
  95.   char z = SPDR;
  96.   return(z);
  97. }
  98.  
  99. void setup(void)
  100. {
  101.   DDRD |= (1<<D7);
  102.   DDRD |= (1<<D6);
  103.   DDRD |= (1<<D5);
  104.   DDRD |= (1<<D4);
  105.   DDRD |= (1<<E);
  106.   DDRD |= (1<<RS);
  107.   DDRD |= (1<<RW);
  108.   PORTD &= ~(1<<RW);
  109.   PORTD &= ~(1<<RS);  
  110.   PORTD &= ~(1<<E);
  111.   ini_lcd();
  112.   PORTD |= (1<<RS);
  113.   _delay_ms(1);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. void loop(void)
  120. {
  121.   PORTD &= ~(1<<RS);
  122.   lcd_bajt(0x80);
  123.  
  124.   PORTD |= (1<<RS);
  125.  
  126.  
  127.  
  128. }
  129.  
  130. void TWIInit(void)
  131. {
  132.   //set SCL to 400kHz
  133.   TWSR = 0x00;
  134.   TWBR = 0x0C;
  135.   //enable TWI
  136.   TWCR = (1<<TWEN);
  137. }
  138. void TWIStart(void)
  139. {
  140.   TWCR =
  141.   (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  142.   while ((TWCR & (1<<TWINT)) == 0);
  143. }
  144. void TWIStop(void)
  145. {
  146.   TWCR =
  147.   (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
  148. }
  149. void TWIWrite(uint8_t u8data)
  150. {
  151.   TWDR = u8data;
  152.   TWCR = (1<<TWINT)|(1<<TWEN);
  153.   while ((TWCR & (1<<TWINT)) == 0);
  154. }
  155. uint8_t TWIReadACK(void)
  156. {
  157.   TWCR =
  158.   (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
  159.   while ((TWCR & (1<<TWINT)) == 0);
  160.   return TWDR;
  161. }
  162. uint8_t TWIReadNACK(void)
  163. {
  164.   TWCR = (1<<TWINT)|(1<<TWEN);
  165.   while ((TWCR & (1<<TWINT)) == 0);
  166.   return TWDR;
  167. }
  168. uint8_t TWIGetStatus(void)
  169. {
  170.   uint8_t status;
  171.   //mask status
  172.   status = TWSR & 248;
  173.   return status;
  174. }
  175.  
  176. uint8_t EEWriteByte(uint16_t u16addr, uint8_t u8data)
  177. {
  178.   TWIStart();
  179.   if (TWIGetStatus() != 8)
  180.   return ERROR;
  181.   //select device and send A2 A1 A0 address bits
  182.   TWIWrite((EEDEVADR)|(uint8_t)((u16addr & 0x0700)>>7));
  183.   if (TWIGetStatus() != 24)
  184.   return ERROR;
  185.   //send the rest of address
  186.   TWIWrite((uint8_t)(u16addr));
  187.   if (TWIGetStatus() != 40)
  188.   return ERROR;
  189.   //write byte to eeprom
  190.   TWIWrite(u8data);
  191.   if (TWIGetStatus() != 40)
  192.   return ERROR;
  193.   TWIStop();
  194.   return SUCCESS;
  195. }
  196.  
  197. uint8_t EEReadByte(uint16_t u16addr)
  198. {
  199.   //uint8_t databyte;
  200.   uint8_t sdata;
  201.   TWIStart();
  202.   if (TWIGetStatus() != 8)
  203.   return ERROR;
  204.   //select device and send A2 A1 A0 address bits
  205.   TWIWrite((EEDEVADR)|((uint8_t)((u16addr & 0x0700)>>7)));
  206.   if (TWIGetStatus() != 36)
  207.   return ERROR;
  208.   //send the rest of address
  209.   TWIWrite((uint8_t)(u16addr));
  210.   if (TWIGetStatus() != 40)
  211.   return ERROR;
  212.   //send start
  213.   TWIStart();
  214.   if (TWIGetStatus() != 16)
  215.   return ERROR;
  216.   //select device and send read bit
  217.   TWIWrite((EEDEVADR)|((uint8_t)((u16addr & 0x0700)>>7))|1);
  218.   if (TWIGetStatus() != 64)
  219.   return ERROR;
  220.   sdata = TWIReadNACK();
  221.   if (TWIGetStatus() != 88)
  222.   return ERROR;
  223.   TWIStop();
  224.   return sdata;
  225.   }
  226.  
  227. int main(void)
  228. {
  229.   TWIInit();
  230.   uint8_t ddata = 0;
  231.   uint8_t ew;
  232.   setup();
  233.   ew= EEWriteByte(5, 'b');
  234.   if(ew==SUCCESS)
  235.   {
  236.     lcd_bajt('S');  
  237.   }
  238.   else lcd_bajt('E');
  239.   _delay_ms(10);
  240.   ddata = EEReadByte(5);
  241.   if(ew==ERROR)
  242.   {
  243.   lcd_bajt('E');
  244.   }
  245.   else lcd_bajt('S');
  246.  
  247.   lcd_bajt(ddata);
  248.   while(1)
  249.   loop();
  250.   return 0;
  251.  
  252. }
  253.