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

Replies to Projekt rss

Title Name Language When
Re: Projekt Krzysztof Karolak c 3 Years ago.