uwezi

20210110_tian_ma_A2C00096100.c

Jan 9th, 2021
917
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * 20210107_m32u4_tian_pollin_2.c
  3.  *
  4.  * Created: 2021-01-07 22:07:56
  5.  * Author : uwezi
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. #include "i2cmaster.h"
  11.  
  12. uint8_t adresses[20] = {0x1A,0x1B,0x1C,0x1E,0x1F,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0E,0x0F};
  13.  
  14. uint8_t hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  15. void init(void)
  16. {
  17.   DDRE = (1 << PE6);
  18.  
  19.   //m_usb_init();
  20.   //while(!m_usb_isconnected()); // wait for a connection
  21.  
  22.   i2c_init(1,152);
  23.  
  24.   DDRB = (1 << PB5);
  25.   PORTB |= (1 << PB5);
  26.   _delay_ms(10);
  27.   PORTB &= ~(1 << PB5);
  28.   _delay_ms(100);
  29.  
  30.   i2c_start_wait(0x74);
  31.   i2c_write(0b00000000);    // CO=0 RS=0 last control byte, instruction
  32.                             // M=0 1-line
  33.                             // SL=0 1:18 multiplex
  34.                             // H=0 basic instruction set
  35.   i2c_write(0b00100000);    // function_set DL=0 M=0 SL=0 H=0
  36.                             // I_D=1 auto address increment
  37.                             // S=0 no display shift
  38.   i2c_write(0b00000110);    // entry_mode I_D=1 S=0
  39.                             // D=1 display on
  40.                             // C=1 cursor is on
  41.                             // B=0 blink is off
  42.   i2c_write(0b00001110);    // display_ctl D=1 C=0 B=0
  43.  
  44.   i2c_write(0b00100001);    // function_set DL=0 M=0 SL=0 H=1
  45.                             // P=0 column data left-to-right, no mirror
  46.                             // Q=0 row top-to-bottom, icons in row 17 & 18
  47.   i2c_write(0b00000100);    // disp_conf P=0 Q=0
  48.                             // S1=1 S0=0 4x voltage multiplier  
  49.   i2c_write(0b01000010);    // hv_gen S1=1 S0=0
  50.                             // IM=0 character mode, full display
  51.                             // IB=0 icon blink disabled, icon data in cgram[0...3] 5x8 bit
  52.                             // DM=0 direct mode off
  53.   i2c_write(0b00001000);    // icon_ctl IM=0 IB=0 DM=0
  54.   i2c_write(0b10110000);    // vlcd_set V=0 VA=0x30
  55.   i2c_write(0b11000000);    // vlcd_set V=1 VB=0x00
  56.   i2c_write(0b00100000);    // function_set DL=0 M=0 SL=0 H=0
  57.   i2c_write(0b00000001);    // clear display
  58.   i2c_stop();
  59. }
  60.  
  61.  
  62. int main(void)
  63. {
  64.   uint8_t j, k;
  65.   init();
  66.  
  67.   while(1)
  68.   {
  69.     // all DDRAM to character #0
  70.     i2c_start_wait(0x74);
  71.     for (k=0; k<128; k++)
  72.     {
  73.       i2c_write(0b10000000);   // one instruction followed by another command
  74.       i2c_write(0b10000000 | k); //DDRAM address
  75.       i2c_write(0b11000000);   // one data followed by another command
  76.       i2c_write(0);
  77.     }
  78.     i2c_stop();
  79.    
  80.     // fill CGRAM
  81.     i2c_start_wait(0x74);
  82.     for (k=0; k<128; k++)
  83.     {
  84.  
  85.       i2c_write(0b10000000);   // one instruction followed by another command
  86.       i2c_write(0b10000000 | (k & 64)); //DDRAM address, highest bit
  87.       i2c_write(0b10000000);   // one instruction followed by another command
  88.       i2c_write(0b01000000 | (k%64)); //CGRAM address
  89.       i2c_write(0b11000000);   // one data followed by another command
  90.       i2c_write(0xff);
  91.     }
  92.     i2c_stop();
  93.  
  94.     _delay_ms(3000);
  95.    
  96.     // all DDRAM to space
  97.     i2c_start_wait(0x74);
  98.     for (k=0; k<128; k++)
  99.     {
  100.       i2c_write(0b10000000);   // one instruction followed by another command
  101.       i2c_write(0b10000000 | k); //DDRAM address
  102.       i2c_write(0b11000000);   // one data followed by another command
  103.       i2c_write(0x20);
  104.     }
  105.  
  106.     for (k=0; k<8; k++)
  107.     {
  108.  
  109.       i2c_write(0b10000000);   // one instruction followed by another command
  110.       i2c_write(0b10000000 | (k & 64)); //DDRAM address, highest bit
  111.       i2c_write(0b10000000);   // one instruction followed by another command
  112.       i2c_write(0b01000000 | (k%64)); //CGRAM address
  113.       i2c_write(0b11000000);   // one data followed by another command
  114.       i2c_write(0);
  115.     }
  116.    
  117.     i2c_write(0b10000000);   // one instruction followed by another command
  118.     i2c_write(0b10000000 | 0x0d); //DDRAM address
  119.     i2c_write(0b11000000);   // one data followed by another command
  120.     i2c_write(0x00);
  121.  
  122.     for (k=0; k<8; k++)
  123.     {
  124.       i2c_start_wait(0x74);
  125.       i2c_write(0b10000000);   // one instruction followed by another command
  126.       i2c_write(0b10000000 | adresses[0]); //DDRAM address
  127.       i2c_write(0b11000000);   // one data followed by another command
  128.       i2c_write(hex[k/16]);
  129.       i2c_write(0b10000000);   // one instruction followed by another command
  130.       i2c_write(0b10000000 | adresses[1]); //DDRAM address
  131.       i2c_write(0b11000000);   // one data followed by another command
  132.       i2c_write(hex[k%16]);
  133.       i2c_write(0b10000000);   // one instruction followed by another command
  134.       i2c_write(0b10000000 | adresses[2]); //DDRAM address
  135.       i2c_write(0b11000000);   // one data followed by another command
  136.       i2c_write(':');
  137.  
  138.       for (j=0;j<8;j++)
  139.       {
  140.         i2c_write(0b10000000);   // one instruction followed by another command
  141.         i2c_write(0b10000000 | adresses[3]); //DDRAM address
  142.         i2c_write(0b11000000);   // one data followed by another command
  143.         i2c_write(j%10 + '0');
  144.  
  145.         i2c_write(0b10000000);   // one instruction followed by another command
  146.         i2c_write(0b10000000 | (k & 64)); //DDRAM address, highest bit
  147.         i2c_write(0b10000000);   // one instruction followed by another command
  148.         i2c_write(0b01000000 | (k%64)); //CGRAM address
  149.         i2c_write(0b11000000);   // one data followed by another command
  150.         i2c_write(1 << j);
  151.         _delay_ms(1000);
  152.       }
  153.     }
  154.  
  155.     i2c_stop();    
  156.  
  157.     _delay_ms(3000);
  158.        
  159.     // clear CGRAM
  160.     i2c_start_wait(0x74);    
  161.     for (k=0; k<128; k++)
  162.     {
  163.  
  164.       i2c_write(0b10000000);   // one instruction followed by another command
  165.       i2c_write(0b10000000 | (k & 64)); //DDRAM address, highest bit
  166.       i2c_write(0b10000000);   // one instruction followed by another command
  167.       i2c_write(0b01000000 | (k%64)); //CGRAM address
  168.       i2c_write(0b11000000);   // one data followed by another command
  169.       i2c_write(0);
  170.     }
  171.     i2c_stop();
  172.    
  173.     i2c_start_wait(0x74);
  174.     for (k=0; k<20; k++)
  175.     {
  176.       i2c_write(0b10000000);   // one instruction followed by another command
  177.       i2c_write(0b10000000 | adresses[k]); //DDRAM address
  178.       i2c_write(0b11000000);   // one data followed by another command
  179.       i2c_write(k%10+'0');
  180.       _delay_ms(500);
  181.     }
  182.     i2c_stop();
  183.     _delay_ms(3000);
  184.  
  185.     _delay_ms(2000);
  186.    
  187.     for (k=0; k<16; k++)
  188.     {
  189.       i2c_start_wait(0x74);
  190.       i2c_write(0b10000000);   // one instruction followed by another command
  191.       i2c_write(0b10000000 | adresses[0]); //DDRAM address
  192.       i2c_write(0b11000000);   // one data followed by another command
  193.       i2c_write(hex[k]);
  194.       i2c_write(0b10000000);   // one instruction followed by another command
  195.       i2c_write(0b10000000 | adresses[1]); //DDRAM address
  196.       i2c_write(0b11000000);   // one data followed by another command
  197.       i2c_write('0');
  198.       i2c_write(0b10000000);   // one instruction followed by another command
  199.       i2c_write(0b10000000 | adresses[2]); //DDRAM address
  200.       i2c_write(0b11000000);   // one data followed by another command
  201.       i2c_write(':');
  202.       i2c_write(0b10000000);   // one instruction followed by another command
  203.       i2c_write(0b10000000 | adresses[3]); //DDRAM address
  204.       i2c_write(0b11000000);   // one data followed by another command
  205.       i2c_write(' ');
  206.  
  207.       for (j=0;j<16;j++)
  208.       {
  209.         i2c_write(0b10000000);   // one instruction followed by another command
  210.         i2c_write(0b10000000 | adresses[4+j]); //DDRAM address
  211.         i2c_write(0b11000000);   // one data followed by another command
  212.         i2c_write(j+16*k);
  213.       }
  214.       i2c_stop();
  215.       _delay_ms(3000);
  216.     }
  217.  
  218.     _delay_ms(2000);    
  219.  
  220.     i2c_start_wait(0x74);
  221.     for (k=0; k<16; k++)
  222.     {
  223.       i2c_write(0b10000000);   // one instruction followed by another command
  224.       i2c_write(0b10000000 | adresses[k+4]); //DDRAM address
  225.       i2c_write(0b11000000);   // one data followed by another command
  226.       i2c_write(k);
  227.     }
  228.     i2c_stop();
  229.    
  230.     for (k=0; k<128; k++)
  231.     {
  232.       i2c_start_wait(0x74);
  233.       i2c_write(0b10000000);   // one instruction followed by another command
  234.       i2c_write(0b10000000 | adresses[0]); //DDRAM address
  235.       i2c_write(0b11000000);   // one data followed by another command
  236.       i2c_write(hex[k/16]);
  237.       i2c_write(0b10000000);   // one instruction followed by another command
  238.       i2c_write(0b10000000 | adresses[1]); //DDRAM address
  239.       i2c_write(0b11000000);   // one data followed by another command
  240.       i2c_write(hex[k%16]);
  241.       i2c_write(0b10000000);   // one instruction followed by another command
  242.       i2c_write(0b10000000 | adresses[2]); //DDRAM address
  243.       i2c_write(0b11000000);   // one data followed by another command
  244.       i2c_write(':');
  245.  
  246.       for (j=0;j<8;j++)
  247.       {
  248.         i2c_write(0b10000000);   // one instruction followed by another command
  249.         i2c_write(0b10000000 | adresses[3]); //DDRAM address
  250.         i2c_write(0b11000000);   // one data followed by another command
  251.         i2c_write(j%10 + '0');
  252.  
  253.         i2c_write(0b10000000);   // one instruction followed by another command
  254.         i2c_write(0b10000000 | (k & 64)); //DDRAM address, highest bit
  255.         i2c_write(0b10000000);   // one instruction followed by another command
  256.         i2c_write(0b01000000 | (k%64)); //CGRAM address
  257.         i2c_write(0b11000000);   // one data followed by another command
  258.         i2c_write(1 << j);
  259.         _delay_ms(250);
  260.       }
  261.  
  262.       i2c_stop();
  263.     }
  264.  
  265.     _delay_ms(2000);
  266.    
  267.   }
  268.   return 0;
  269. }
RAW Paste Data