Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <stdio.h>
- #include <util/delay.h>
- #include <string.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
- #include <compat/twi.h>
- #define MAX_TRIES 50
- #define MCP23008_ID 0x40 // MCP23008 Device Identifier
- #define MCP23008_ADDR 0x00 // MCP23008 Device Address
- #define GPIO 0x09 // MCP23008 General Purpose I/O Register
- #define OLAT 0x0A // MCP23008 Output Latch Register
- #define I2C_START 0
- #define I2C_DATA 1
- #define I2C_DATA_ACK 2
- #define I2C_STOP 3
- #define ACK 1
- #define NACK 0
- #define DATASIZE 32
- #define SS 2
- #define SS2 1
- #define MOSI 3
- #define MISO 4
- #define SCK 5
- #define TXD 1
- #define ENABLE_SS PORTB &= ~(1 << SS)
- #define DISABLE_SS PORTB |= (1 << SS)
- #define ENABLE_SS2 PORTB &= ~(1 << PORTB1) //pin 9 arduino = pin portb1
- #define DISABLE_SS2 PORTB |= (1 << PORTB1)
- #define FOSC 16000000
- #define BAUD 9600
- #define MYUBBR FOSC/16/BAUD -1
- #define UART_BUFF_SIZE 10
- #define A 0
- #define B 1
- #define READ 1
- #define WRITE 0
- #define IODIR 0
- #define OUTP_LATCH 10
- void USART_Init(void);
- void init(void);
- uint8_t spi_tranceiver (uint8_t data);
- void USART_Transmit(uint8_t data);
- uint8_t adcSender(uint8_t data);
- int16_t Temp_read(void);
- void IOEXP_IODIR(uint8_t data);
- void IOEXP_datalatch(uint8_t data);
- uint8_t RX_buf[UART_BUFF_SIZE];
- uint8_t TX_buf[UART_BUFF_SIZE];
- uint8_t RX_index = 0;
- uint8_t TX_index = 0;
- ISR(USART_RX_vect)
- {
- RX_buf[RX_index++] = UDR0;
- USART_Transmit(RX_buf[RX_index-1]);
- }
- int main(void)
- {
- PORTC |= 0B00110000;
- init();
- i2c_init();
- /* Clearing buffers */
- memset(RX_buf,0,sizeof(RX_buf));
- memset(TX_buf,0,sizeof(TX_buf));
- Write_MCP23008(IODIR,0b00000000);
- Write_MCP23008(GPIO,0b00000000); // Reset all the Output Port
- /* Enable interrupts */
- sei();
- /*
- /* set all pins of I/0 expander to output */
- //IOEXP_IODIR(0);
- /* set lower 4 bits (leds) high*/
- // IOEXP_datalatch(0x0f);
- //_delay_ms(1000);
- /* set higher 4 bits (leds) high
- IOEXP_datalatch(0xf0);
- _delay_ms(1000);*/
- while (1)
- {
- int8_t data = adcSender(0xf00);
- //IOEXP_datalatch(data); //set led
- //USART_Transmit('t');
- //USART_Transmit('=');
- USART_Transmit(data/10+48);
- USART_Transmit(data%10+48);
- USART_Transmit("/r");
- Write_MCP23008(GPIO,data);
- //USART_Transmit(data); // give same value to USART
- _delay_ms(2000);
- }
- }
- void init()
- {
- USART_Init();
- /* Set SS, MOSI and SCK output, all others input */
- DDRB = (1<<SS)|(1<<MOSI)|(1<<SCK)|(1 << PORTB1);
- /* LED PIN */
- DDRB |= (1 << PORTB0);
- /* Set TXD as an output */
- DDRD = (1 << TXD);
- /* Set the slave select pin (Active low) */
- DISABLE_SS;
- DISABLE_SS2;
- /* Enable SPI, Master, set clock rate fosc/16 */
- SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
- }
- // function to initialize UART
- void USART_Init(void)
- {
- // Set baud rate:
- UBRR0=103; //UBRR= Fosc /(16*9600) -1 =103.166= 103
- // enable receiver and transmitter
- UCSR0B |=(1<<RXEN0 |(1 <<TXEN0));
- // Set frame format : 8 data 2 stop bit
- UCSR0C = (1<<USBS0 )|(3<<UCSZ00);
- }
- void USART_Transmit( uint8_t data )
- {
- /* Wait for empty transmit buffer */
- while ( !( UCSR0A & (1<<UDRE0)) );
- /* Put data into buffer, sends the data */
- UDR0 = data;
- }
- /* spi data buffer load and send */
- uint8_t spi_tranceiver (uint8_t data)
- {
- // Load data into the buffer
- SPDR = data;
- //Wait until transmission complete
- while(!(SPSR & (1<<SPIF) ));
- // Return received data
- return(SPDR);
- }
- uint8_t adcSender(uint8_t data)
- {
- ENABLE_SS2;
- SPDR = data;
- while(!(SPSR & (1<<SPIF) ));
- //Read first 7 bits then second 5 bits
- uint8_t val0 = SPDR;
- uint8_t val1 = SPDR;
- DISABLE_SS2;
- unsigned short returnByte;
- uint16_t total0 = val0;
- returnByte = (total0 << 8);
- returnByte = returnByte | val1;
- returnByte = returnByte << 1;
- uint8_t totaleight = (returnByte >> 8);
- return totaleight;
- }
- unsigned char i2c_transmit(unsigned char type) {
- switch(type) {
- case I2C_START: // Send Start Condition
- TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
- break;
- case I2C_DATA: // Send Data with No-Acknowledge
- TWCR = (1 << TWINT) | (1 << TWEN);
- break;
- case I2C_DATA_ACK: // Send Data with Acknowledge
- TWCR = (1 << TWEA) | (1 << TWINT) | (1 << TWEN);
- break;
- case I2C_STOP: // Send Stop Condition
- TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
- return 0;
- }
- // Wait for TWINT flag set on Register TWCR
- while (!(TWCR & (1 << TWINT)));
- // Return TWI Status Register, mask the prescaler bits (TWPS1,TWPS0)
- return (TWSR & 0xF8);
- }
- char i2c_start(unsigned int dev_id, unsigned int dev_addr, unsigned char rw_type)
- {
- unsigned char n = 0;
- unsigned char twi_status;
- char r_val = -1;
- i2c_retry:
- if (n++ >= MAX_TRIES) return r_val;
- // Transmit Start Condition
- twi_status=i2c_transmit(I2C_START);
- // Check the TWI Status
- if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
- if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;
- // Send slave address (SLA_W)
- TWDR = (dev_id & 0xF0) | (dev_addr & 0x0E) | rw_type;
- // Transmit I2C Data
- twi_status=i2c_transmit(I2C_DATA);
- // Check the TWSR status
- if ((twi_status == TW_MT_SLA_NACK) || (twi_status == TW_MT_ARB_LOST)) goto i2c_retry;
- if (twi_status != TW_MT_SLA_ACK) goto i2c_quit;
- r_val=0;
- i2c_quit:
- return r_val;
- }
- void i2c_stop(void)
- {
- unsigned char twi_status;
- // Transmit I2C Data
- twi_status=i2c_transmit(I2C_STOP);
- }
- char i2c_write(char data)
- {
- unsigned char twi_status;
- char r_val = -1;
- // Send the Data to I2C Bus
- TWDR = data;
- // Transmit I2C Data
- twi_status=i2c_transmit(I2C_DATA);
- // Check the TWSR status
- if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;
- r_val=0;
- i2c_quit:
- return r_val;
- }
- void Write_MCP23008(unsigned char reg_addr,unsigned char data)
- {
- // Start the I2C Write Transmission
- i2c_start(MCP23008_ID,MCP23008_ADDR,TW_WRITE);
- // Sending the Register Address
- i2c_write(reg_addr);
- // Write data to MCP23008 Register
- i2c_write(data);
- // Stop I2C Transmission
- i2c_stop();
- }
- void i2c_init(void)
- {
- // Initial ATMega328P TWI/I2C Peripheral
- TWSR = 0x00; // Select Prescaler of 1
- // SCL frequency = 11059200 / (16 + 2 * 48 * 1) = 98.743 kHz
- TWBR = 0x30; // 48 Decimal
- }
- /* IO expander latch set */
- void IOEXP_datalatch(uint8_t data)
- {
- // Make slave select go low
- ENABLE_SS;
- /* Send device address + r/w */
- spi_tranceiver((1<<6)|WRITE);
- /* Send command */
- spi_tranceiver(OUTP_LATCH);
- spi_tranceiver(data);
- // Make slave select go high
- DISABLE_SS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement