SHARE
TWEET

Untitled

a guest Apr 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define F_CPU 16000000UL
  2. #include <avr/io.h>
  3. #include <stdio.h>
  4. #include <util/delay.h>
  5. #include <string.h>
  6. #include <avr/interrupt.h>
  7. #include <stdlib.h>
  8. #include <compat/twi.h>
  9. #define MAX_TRIES 50
  10. #define MCP23008_ID    0x40  // MCP23008 Device Identifier
  11. #define MCP23008_ADDR  0x00  // MCP23008 Device Address
  12. #define GPIO  0x09           // MCP23008 General Purpose I/O Register
  13. #define OLAT  0x0A           // MCP23008 Output Latch Register
  14. #define I2C_START 0
  15. #define I2C_DATA 1
  16. #define I2C_DATA_ACK 2
  17. #define I2C_STOP 3
  18. #define ACK 1
  19. #define NACK 0
  20. #define DATASIZE 32
  21.  
  22. #define SS 2
  23. #define SS2 1
  24. #define MOSI 3
  25. #define MISO 4
  26. #define SCK 5
  27. #define TXD 1
  28.  
  29. #define ENABLE_SS PORTB &= ~(1 << SS)
  30. #define DISABLE_SS PORTB |= (1 << SS)
  31. #define ENABLE_SS2 PORTB &= ~(1 << PORTB1)  //pin 9 arduino = pin portb1
  32. #define DISABLE_SS2 PORTB |= (1 << PORTB1)
  33.  
  34. #define  FOSC 16000000
  35. #define  BAUD 9600
  36. #define  MYUBBR FOSC/16/BAUD -1
  37.  
  38. #define UART_BUFF_SIZE 10
  39.  
  40.  
  41. #define A 0
  42. #define B 1
  43.  
  44. #define READ 1
  45. #define WRITE 0
  46.  
  47. #define IODIR 0
  48. #define OUTP_LATCH 10
  49.  
  50. void USART_Init(void);
  51. void init(void);
  52. uint8_t spi_tranceiver (uint8_t data);
  53. void USART_Transmit(uint8_t data);
  54. uint8_t adcSender(uint8_t data);
  55.  
  56. int16_t Temp_read(void);
  57. void IOEXP_IODIR(uint8_t data);
  58. void IOEXP_datalatch(uint8_t data);
  59.  
  60. uint8_t RX_buf[UART_BUFF_SIZE];
  61. uint8_t TX_buf[UART_BUFF_SIZE];
  62. uint8_t RX_index = 0;
  63. uint8_t TX_index = 0;
  64.  
  65. ISR(USART_RX_vect)
  66. {
  67.     RX_buf[RX_index++] = UDR0;
  68.     USART_Transmit(RX_buf[RX_index-1]);
  69. }
  70.  
  71. int main(void)
  72. {
  73.     PORTC |= 0B00110000;
  74.     init();
  75.     i2c_init();
  76.     /* Clearing buffers */
  77.     memset(RX_buf,0,sizeof(RX_buf));
  78.     memset(TX_buf,0,sizeof(TX_buf));
  79.     Write_MCP23008(IODIR,0b00000000);
  80.     Write_MCP23008(GPIO,0b00000000);    // Reset all the Output Port
  81.    
  82.     /* Enable interrupts */
  83.     sei();
  84.     /*
  85.     /* set all pins of I/0 expander to output */
  86.     //IOEXP_IODIR(0);
  87.     /* set lower 4 bits (leds) high*/
  88. //  IOEXP_datalatch(0x0f);
  89.     //_delay_ms(1000);
  90.     /* set higher 4 bits (leds) high
  91.     IOEXP_datalatch(0xf0);
  92.     _delay_ms(1000);*/
  93.    
  94.     while (1)
  95.     {
  96.         int8_t data = adcSender(0xf00);
  97.        
  98.         //IOEXP_datalatch(data); //set led
  99.             //USART_Transmit('t');
  100.             //USART_Transmit('=');
  101.             USART_Transmit(data/10+48);
  102.             USART_Transmit(data%10+48);
  103.             USART_Transmit("/r");
  104.             Write_MCP23008(GPIO,data);
  105.         //USART_Transmit(data);    // give same value to USART
  106.         _delay_ms(2000);
  107.     }
  108. }
  109.  
  110. void init()
  111. {
  112.     USART_Init();
  113.     /* Set SS, MOSI and SCK output, all others input */
  114.     DDRB = (1<<SS)|(1<<MOSI)|(1<<SCK)|(1 << PORTB1);
  115.     /* LED PIN */
  116.     DDRB |= (1 << PORTB0);
  117.     /* Set TXD as an output */
  118.     DDRD = (1 << TXD);
  119.    
  120.     /* Set the slave select pin (Active low) */
  121.     DISABLE_SS;
  122.     DISABLE_SS2;
  123.    
  124.     /* Enable SPI, Master, set clock rate fosc/16 */
  125.     SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
  126. }
  127.  
  128.  
  129. // function to initialize UART
  130. void USART_Init(void)
  131. {
  132.     // Set baud rate:
  133.     UBRR0=103;                 //UBRR= Fosc /(16*9600) -1 =103.166= 103
  134.  
  135.     // enable receiver and transmitter
  136.     UCSR0B |=(1<<RXEN0 |(1 <<TXEN0));
  137.  
  138.     // Set frame format : 8 data 2 stop bit
  139.  
  140.     UCSR0C = (1<<USBS0 )|(3<<UCSZ00);
  141. }
  142.  
  143. void USART_Transmit( uint8_t data )
  144. {
  145.     /* Wait for empty transmit buffer */
  146.     while ( !( UCSR0A & (1<<UDRE0)) );
  147.     /* Put data into buffer, sends the data */
  148.     UDR0 = data;
  149. }
  150.  
  151. /* spi data buffer load and send */
  152. uint8_t spi_tranceiver (uint8_t data)
  153. {
  154.     // Load data into the buffer
  155.     SPDR = data;
  156.    
  157.     //Wait until transmission complete
  158.     while(!(SPSR & (1<<SPIF) ));
  159.    
  160.     // Return received data
  161.     return(SPDR);
  162. }
  163.  
  164. uint8_t adcSender(uint8_t data)
  165. {
  166.     ENABLE_SS2;
  167.    
  168.     SPDR = data;
  169.     while(!(SPSR & (1<<SPIF) ));
  170.    
  171.     //Read first 7 bits then second 5 bits
  172.     uint8_t val0 = SPDR;
  173.     uint8_t val1 = SPDR;
  174.     DISABLE_SS2;
  175.     unsigned short returnByte;
  176.    
  177.     uint16_t total0 = val0;
  178.     returnByte = (total0 << 8);
  179.     returnByte = returnByte | val1;
  180.     returnByte = returnByte << 1;
  181.    
  182.     uint8_t totaleight = (returnByte >> 8);
  183.     return totaleight;
  184. }
  185.  
  186.  
  187. unsigned char i2c_transmit(unsigned char type) {
  188.     switch(type) {
  189.         case I2C_START:    // Send Start Condition
  190.         TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  191.         break;
  192.         case I2C_DATA:     // Send Data with No-Acknowledge
  193.         TWCR = (1 << TWINT) | (1 << TWEN);
  194.         break;
  195.         case I2C_DATA_ACK: // Send Data with Acknowledge
  196.         TWCR = (1 << TWEA) | (1 << TWINT) | (1 << TWEN);
  197.         break;
  198.         case I2C_STOP:     // Send Stop Condition
  199.         TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  200.         return 0;
  201.     }
  202.     // Wait for TWINT flag set on Register TWCR
  203.     while (!(TWCR & (1 << TWINT)));
  204.     // Return TWI Status Register, mask the prescaler bits (TWPS1,TWPS0)
  205.     return (TWSR & 0xF8);
  206. }
  207. char i2c_start(unsigned int dev_id, unsigned int dev_addr, unsigned char rw_type)
  208. {
  209.     unsigned char n = 0;
  210.     unsigned char twi_status;
  211.     char r_val = -1;
  212.     i2c_retry:
  213.     if (n++ >= MAX_TRIES) return r_val;
  214.     // Transmit Start Condition
  215.     twi_status=i2c_transmit(I2C_START);
  216.  
  217.     // Check the TWI Status
  218.     if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
  219.     if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
  220.     // Send slave address (SLA_W)
  221.     TWDR = (dev_id & 0xF0) | (dev_addr & 0x0E) | rw_type;
  222.     // Transmit I2C Data
  223.     twi_status=i2c_transmit(I2C_DATA);
  224.     // Check the TWSR status
  225.     if ((twi_status == TW_MT_SLA_NACK) || (twi_status == TW_MT_ARB_LOST)) goto i2c_retry;
  226.     if (twi_status != TW_MT_SLA_ACK) goto i2c_quit;
  227.     r_val=0;
  228.     i2c_quit:
  229.     return r_val;
  230. }
  231. void i2c_stop(void)
  232. {
  233.     unsigned char twi_status;
  234.     // Transmit I2C Data
  235.     twi_status=i2c_transmit(I2C_STOP);
  236. }
  237. char i2c_write(char data)
  238. {
  239.     unsigned char twi_status;
  240.     char r_val = -1;
  241.     // Send the Data to I2C Bus
  242.     TWDR = data;
  243.     // Transmit I2C Data
  244.     twi_status=i2c_transmit(I2C_DATA);
  245.     // Check the TWSR status
  246.     if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;
  247.     r_val=0;
  248.     i2c_quit:
  249.     return r_val;
  250. }
  251.  
  252. void Write_MCP23008(unsigned char reg_addr,unsigned char data)
  253. {
  254.     // Start the I2C Write Transmission
  255.     i2c_start(MCP23008_ID,MCP23008_ADDR,TW_WRITE);
  256.     // Sending the Register Address
  257.     i2c_write(reg_addr);
  258.     // Write data to MCP23008 Register
  259.     i2c_write(data);
  260.     // Stop I2C Transmission
  261.     i2c_stop();
  262. }
  263.  
  264.  
  265. void i2c_init(void)
  266. {
  267.     // Initial ATMega328P TWI/I2C Peripheral
  268.     TWSR = 0x00;         // Select Prescaler of 1
  269.     // SCL frequency = 11059200 / (16 + 2 * 48 * 1) = 98.743 kHz
  270.     TWBR = 0x30;        // 48 Decimal
  271. }
  272.  
  273. /* IO expander latch  set */
  274. void IOEXP_datalatch(uint8_t data)
  275. {
  276.     // Make slave select go low
  277.     ENABLE_SS;
  278.     /* Send device address + r/w */
  279.     spi_tranceiver((1<<6)|WRITE);
  280.     /* Send command */
  281.     spi_tranceiver(OUTP_LATCH);
  282.     spi_tranceiver(data);
  283.     // Make slave select go high
  284.     DISABLE_SS;
  285.    
  286. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top