Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Defines */
- #define F_CPU 16000000UL
- #define FOSC 16000000 // Clock Speed
- #define BAUD 9600
- #define MYUBRR FOSC/16/BAUD-1
- #define USART_RX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
- #define USART_TX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
- #define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
- #define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
- #define I2C_READ 0x01
- #define I2C_WRITE 0x00
- #define text "Hello from Arduino!\n\nThis project contains a variety of different functionalities.\nBy going through the menu you can easily setup and control the system.\n"
- /* Preprocessor */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <string.h>
- #include <stdio.h>
- #include <util/delay.h>
- #include <MyLib/I2Cmaster/i2c_master.h>
- #include <util/twi.h>
- #include "i2c_master.h"
- #define F_SCL 16000000UL // SCL frequency
- #define Prescaler 1
- #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
- /* Function Prototypes */
- void ReceiveSystemInstructions( char* Buffer, unsigned int BufferSize );
- void USART_Transmit_IO( char data, FILE *stream );
- void USART_Init( unsigned int baudrate );
- void USART_Transmit( unsigned char data);
- void USART_PutString(char* StringPointer);
- void menu(int menuposition);
- void sendADCValue(void);
- void readLightingSensor(void);
- void TemperatureSensor(void);
- void i2c_stop(void);
- void i2c_init(void);
- uint8_t i2c_start(uint8_t address)
- uint8_t i2c_write(uint8_t data)
- uint8_t i2c_read_ack(void)
- uint8_t i2c_read_nack(void)
- uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
- uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
- uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
- uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
- void i2c_stop(void)
- /* Buffers */
- char USART_ReceiveBuffer[USART_RX_BUFFER_SIZE];
- static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
- char USART_Instruction[32];
- /* Static Variables */
- static volatile unsigned char WritingPositionOnTheBuffer;
- static volatile unsigned char ReadingPositionOnTheBuffer;
- static volatile unsigned char USART_TxHead;
- static volatile unsigned char USART_TxTail;
- static FILE mystdout = FDEV_SETUP_STREAM(USART_Transmit_IO, NULL, _FDEV_SETUP_WRITE);
- /* String Declarations */
- char String[] = text;
- /* Flags */
- volatile uint8_t adcflag = 0;
- volatile uint8_t i2cflag = 1;
- /* Global Variables */
- uint8_t i2c_start(uint8_t address);
- uint8_t i2c_write(uint8_t data);
- uint8_t i2c_read_ack(void);
- uint8_t i2c_read_nack(void);
- uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length);
- uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length);
- uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length);
- uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length);
- int main(void)
- {
- /* Outputs */
- DDRD |= (1<<PIND7)|(1<<PIND6)|(1<<PIND5)|(1<<PIND4)|(1<<PIND3)|(1<<PIND2);
- /* Button ADC conversion start/stop */
- DDRB &= ~(1<<PINB0);
- /* Pull up resistor */
- PORTB |= (1<<PINB0);
- /* ADC */
- ADCSRA |= 1<<ADPS2;
- ADMUX |= 1<<ADLAR;
- ADMUX |= 1<<REFS0;
- ADCSRA |= 1<<ADIE;
- ADCSRA |= 1<<ADEN;
- /* Global Interrupts Enabled */
- sei();
- /* USART */
- USART_Init( MYUBRR );
- stdout = &mystdout;
- /* I2C */
- i2c_init();
- USART_PutString(String);
- menu(0);
- /* Menu Flags */
- int pmenu = 0;
- int LEDs = 0;
- int LEDMenuMessage = 1;
- /* Forever Loop */
- while(1)
- {
- ReceiveSystemInstructions(USART_Instruction, USART_TX_BUFFER_SIZE);
- if(strcmp(USART_Instruction, "Temperature") == 0) pmenu = 1;
- if(strcmp(USART_Instruction, "Menu") == 0) pmenu = 2;
- if(strcmp(USART_Instruction, "ADC") == 0) pmenu = 3;
- if(strcmp(USART_Instruction, "LED") == 0) pmenu = 4;
- if(strcmp(USART_Instruction, "back") == 0) pmenu = 5;
- if(strcmp(USART_Instruction, "LEDs off") == 0) pmenu = 6;
- if(strcmp(USART_Instruction, "LED 1 on") == 0) pmenu = 7;
- if(strcmp(USART_Instruction, "LED 1 off") == 0) pmenu = 8;
- if(strcmp(USART_Instruction, "LED 2 on") == 0) pmenu = 9;
- if(strcmp(USART_Instruction, "LED 2 off") == 0) pmenu = 10;
- if(strcmp(USART_Instruction, "LED 3 on") == 0) pmenu = 11;
- if(strcmp(USART_Instruction, "LED 3 off") == 0) pmenu = 12;
- if(strcmp(USART_Instruction, "LED 4 on") == 0) pmenu = 13;
- if(strcmp(USART_Instruction, "LED 4 off") == 0) pmenu = 14;
- if(strcmp(USART_Instruction, "LED 5 on") == 0) pmenu = 15;
- if(strcmp(USART_Instruction, "LED 5 off") == 0) pmenu = 16;
- if(strcmp(USART_Instruction, "LED 6 on") == 0) pmenu = 17;
- if(strcmp(USART_Instruction, "LED 6 off") == 0) pmenu = 18;
- if(strcmp(USART_Instruction, " ") == 0) printf("\n");
- if(pmenu > 18) // Needs to be continued
- {
- printf("Invalid Command\n");
- }
- switch(pmenu)
- {
- case 1: i2cflag = 1;
- TemperatureSensor();
- break;
- case 2: menu(0);
- pmenu = 0;
- break;
- case 3: sendADCValue();
- pmenu = 0;
- break;
- case 4: LEDs = 1;
- pmenu = 0;
- break;
- }
- if(LEDs)
- {
- menu(LEDMenuMessage);
- LEDMenuMessage = 10; // Clear the flag
- switch(pmenu)
- {
- case 5:
- LEDs = 0; // Clear the flag
- LEDMenuMessage = 1; // Reset the flag
- printf("You are back to the main menu!\n\n");
- menu(0);
- break;
- case 6:
- PORTD &= ~(1<<PIND7);
- _delay_ms(200);
- PORTD &= ~(1<<PIND6);
- _delay_ms(200);
- PORTD &= ~(1<<PIND5);
- _delay_ms(200);
- PORTD &= ~(1<<PIND4);
- _delay_ms(200);
- PORTD &= ~(1<<PIND3);
- _delay_ms(200);
- PORTD &= ~(1<<PIND2);
- _delay_ms(200);
- printf("LED's are off\n");
- break;
- case 7:
- PORTD |= (1<<PIND7);
- printf("LED 1 is on\nTo turn it off type: LED 1 off\n");
- break;
- case 8:
- PORTD &= ~(1<<PIND7);
- printf("LED 1 is turned off\n");
- break;
- case 9:
- PORTD |= (1<<PIND6);
- printf("LED 2 is on\nTo turn it off type: LED 2 off\n");
- break;
- case 10:
- PORTD &= ~(1<<PIND6);
- printf("LED 2 is turned off\n");
- break;
- case 11:
- PORTD |= (1<<PIND5);
- printf("LED 3 is on\nTo turn it off type: LED 3 off\n");
- break;
- case 12:
- PORTD &= ~(1<<PIND5);
- printf("LED 3 is turned off\n");
- break;
- case 13:
- PORTD |= (1<<PIND4);
- printf("LED 4 is on\nTo turn it off type: LED 4 off\n");
- break;
- case 14: {
- PORTD &= ~(1<<PIND4);
- printf("LED 4 is turned off\n");
- break;
- case 15:
- PORTD |= (1<<PIND3);
- printf("LED 5 is on\nTo turn it off type: LED 5 off\n");
- break;
- case 16:
- PORTD &= ~(1<<PIND3);
- printf("LED 5 is turned off\n");
- break;
- case 17:
- PORTD |= (1<<PIND2);
- printf("LED 6 is on\nTo turn it off type: LED 6 off\n");
- break;
- case 18:
- PORTD &= ~(1<<PIND2);
- printf("LED 6 is turned off\n");
- break;
- }
- }
- }
- }
- }
- ISR(ADC_vect)
- {
- adcflag = 1;
- }
- ISR(USART_RX_vect) // Generating data in the Receive Buffer
- {
- if(i2cflag == 1) i2cflag = 0;
- unsigned char data;
- unsigned char tmpWPosition;
- /* Read the received data */
- data = UDR0;
- /* Determine the Write PositionInTheBuffer */
- tmpWPosition = (WritingPositionOnTheBuffer + 1) & USART_RX_BUFFER_MASK;
- /* Store new index */
- WritingPositionOnTheBuffer = tmpWPosition;
- /* Store received data in buffer */
- USART_ReceiveBuffer[tmpWPosition] = data;
- }
- ISR(USART_UDRE_vect)
- {
- unsigned char tmptail;
- /* Check if all data is transmitted */
- if ( USART_TxHead != USART_TxTail )
- {
- /* Calculate buffer index */
- tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;
- USART_TxTail = tmptail; /* Store new index */
- UDR0 = USART_TxBuf[tmptail]; /* Start transmission */
- }
- else
- {
- UCSR0B &= ~(1<<UDRIE0); /* Disable UDRE interrupt */
- }
- }
- void USART_Init( unsigned int baudrate )
- {
- unsigned char x;
- /* Set the baud rate */
- UBRR0H = (unsigned char) (baudrate>>8);
- UBRR0L = (unsigned char) baudrate;
- /* Enable UART receiver and transmitter */
- UCSR0B = ( ( 1 << RXCIE0 ) | ( 1 << RXEN0 ) | ( 1 << TXEN0 ) );
- /* Set frame format: 8 data 1stop */
- UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
- /* Flush receive buffer */
- x = 0;
- ReadingPositionOnTheBuffer = x;
- WritingPositionOnTheBuffer = x;
- USART_TxTail = x;
- USART_TxHead = x;
- }
- void USART_Transmit( unsigned char data) //Simple Transmit Function
- {
- while(!(UCSR0A & (1<<UDRE0))); // Wait
- UDR0 = data;
- }
- void USART_PutString(char* StringPointer)
- {
- while(*StringPointer != 0x00) //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
- {
- USART_Transmit(*StringPointer); //Using the simple send function we send one char at a time
- StringPointer++; //We increment the pointer so we can read the next char
- }
- }
- void ReceiveSystemInstructions( char* Buffer, unsigned int BufferSize )
- {
- unsigned char tmpWPosition;
- unsigned int i = 0;
- int b;
- uint8_t ReadPin;
- while ( WritingPositionOnTheBuffer == ReadingPositionOnTheBuffer ) // Wait for incoming data /
- {
- for(b = 0; b < 200 ; b++)
- {
- _delay_ms(1);
- ReadPin = ( PINB & ( 1 << PINB0 ) ) ? 1 : 0; // Read the pin
- if(!ReadPin)
- {
- printf("Start Conversion\n");
- do
- {
- ADCSRA |= 1<<ADSC; // Start ADC Conversion
- _delay_ms(1000);
- ReadPin = ( PINB & ( 1 << PINB0 ) ) ? 1 : 0;
- readLightingSensor();
- } while (ReadPin == 1);
- printf("Stop Conversion\n");
- _delay_ms(1000);
- }
- }
- b=0;
- }
- _delay_ms(10);
- for( i=0; i < BufferSize - 1; ++i )
- {
- tmpWPosition = ( ReadingPositionOnTheBuffer + 1 ) & USART_RX_BUFFER_MASK; // Calculate buffer index /
- ReadingPositionOnTheBuffer = tmpWPosition; // Store new index /
- Buffer[i] = USART_ReceiveBuffer[tmpWPosition]; // Return data /
- if( WritingPositionOnTheBuffer == ReadingPositionOnTheBuffer )
- break;
- }
- Buffer[i+1] = '\0'; // Adding a Null Terminator
- }
- void menu(int menuposition)
- {
- if(menuposition == 0)
- {
- printf("Temperature sensor type: Temperature\n");
- printf("To access the LEDs type: LED\n");
- printf("To get the ADC Value type: ADC\n");
- printf("Turning off the LED's by typing: LEDs off\n");
- printf("\nTo see the menu again type: Menu\n");
- }
- if(menuposition == 1)
- {
- printf("You are now in the LED section.\n\nTo turn on the LEDn type LED n on, where n goes from 1 to 6: \n\nGo back by typing: back\n");
- }
- }
- void USART_Transmit_IO( char data, FILE *stream )
- {
- unsigned char tmphead;
- /* Calculate buffer index */
- tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK;
- while ( tmphead == USART_TxTail ); /* Wait for free space in buffer */
- USART_TxBuf[tmphead] = data; /* Store data in buffer */
- USART_TxHead = tmphead; /* Store new index */
- UCSR0B |= (1<<UDRIE0); /* Enable UDRE interrupt */
- }
- void sendADCValue(void)
- {
- ADCSRA |= 1<<ADSC; // Start Conversion
- _delay_ms(1); // Wait for the interrupt to complete and then to check the if statement
- if (adcflag)
- {
- uint16_t adcData = ADCH;
- printf("ADC Value: %u\n", adcData);
- adcflag = 0;
- }
- }
- void readLightingSensor(void)
- {
- if (adcflag)
- {
- uint16_t adcData = ADCH;
- printf("%u\n", adcData);
- adcflag = 0;
- }
- }
- void TemperatureSensor(void)
- {
- int data;
- printf("\nYou have requested the stream of temperature sensor values. To cancel the request type: back\n\n");
- printf("Stream of temperature sensor values:\n");
- _delay_ms(100);
- while(i2cflag == 1)
- {
- i2c_start(0b10010001); // Start and Read request
- if(i2c_start(0b10010001)==1) printf("ERROR\n");
- i2c_read_ack();
- data = TWDR;
- printf("%d\n", data);
- _delay_ms(1000);
- }
- }
- void i2c_init(void)
- {
- TWBR = (uint8_t)TWBR_val;
- }
- uint8_t i2c_start(uint8_t address)
- {
- // reset TWI control register
- TWCR = 0;
- // transmit START condition
- TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
- // check if the start condition was successfully transmitted
- if((TWSR & 0xF8) != TW_START){ return 1; }
- // load slave address into data register
- TWDR = address;
- // start transmission of address
- TWCR = (1<<TWINT) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
- // check if the device has acknowledged the READ / WRITE mode
- uint8_t twst = TW_STATUS & 0xF8;
- if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
- return 0;
- }
- uint8_t i2c_write(uint8_t data)
- {
- // load data into data register
- TWDR = data;
- // start transmission of data
- TWCR = (1<<TWINT) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
- if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return ERROR(); }
- return 0;
- }
- uint8_t i2c_read_ack(void)
- {
- // start TWI module and acknowledge data after reception
- TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
- // return received data from TWDR
- return TWDR;
- }
- uint8_t i2c_read_nack(void)
- {
- // start receiving without acknowledging reception
- TWCR = (1<<TWINT) | (1<<TWEN);
- // wait for end of transmission
- while( !(TWCR & (1<<TWINT)) );
- // return received data from TWDR
- return TWDR;
- }
- uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
- {
- if (i2c_start(address | I2C_WRITE)) return ERROR();
- for (uint16_t i = 0; i < length; i++)
- {
- if (i2c_write(data[i])) return ERROR();
- }
- i2c_stop();
- return 0;
- }
- uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
- {
- if (i2c_start(address | I2C_READ)) return 1;
- for (uint16_t i = 0; i < (length-1); i++)
- {
- data[i] = i2c_read_ack();
- }
- data[(length-1)] = i2c_read_nack();
- i2c_stop();
- return 0;
- }
- uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
- {
- if (i2c_start(devaddr | 0x00)) return 1;
- i2c_write(regaddr);
- for (uint16_t i = 0; i < length; i++)
- {
- if (i2c_write(data[i])) return 1;
- }
- i2c_stop();
- return 0;
- }
- uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
- {
- if (i2c_start(devaddr)) return 1;
- i2c_write(regaddr);
- if (i2c_start(devaddr | 0x01)) return 1;
- for (uint16_t i = 0; i < (length-1); i++)
- {
- data[i] = i2c_read_ack();
- }
- data[(length-1)] = i2c_read_nack();
- i2c_stop();
- return 0;
- }
- void i2c_stop(void)
- {
- // transmit STOP condition
- TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement