Can somebody help me??
I've been trying to do a step by step programming, including functions at each step.
Now i'm with a problem... I'm using virtual terminal on Proteus, and everything was Ok till now. On this step i have to send a counting each second in decimal, hexadecimal and binary. I had used itoa function and it was Ok, but i need to do the conversion manually, so i increment a variable each second (by ISR_Timer1) and then i take the variable value and call functions to convert it, then i call another function (SendString) to send the converted data. My problem (now, maybe i get more on future :( ) is: I had converted the data to binary (and i think it's ok, please correct me if i'm wrong) and now i have to place it on a char array and pass the array. What am i doing wrong??? When i simulate, the data appears on the array but it's not sent.
#define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #include <string.h> #include "bit_tools.h" #include <stdlib.h> #define USART_BAUDRATE 250000 // Define usart baud rate #define MYUBRRN ((F_CPU/(USART_BAUDRATE<<4))-1) //define UBRR(usart baud rate register) as UBRRN=11 has i had calculated /////////////////// //GLOBAL VARIABLES/ /////////////////// uint8_t b; // prototipes void initUsart (uint16_t MY_UBBR); void sendChar (uint8_t data); void sendString ( char *data); void configTimer1(); void write_dec(uint8_t b); void write_hex(uint8_t b); void write_bin(uint8_t b); ////////////////////////////////// //Function : Config the timer //////////////////////////////////// void configTimer1() { TCCR1B |= (1<<WGM12); // CTC Mode TIMSK1 = (1<<OCIE1A); // Enable Timer 1 Interrupt OCR1A = 31249; // Load Value calculated to have 1 second / Prescaler = 256 @ 8Mhz TCCR1B |= (1<<CS12); // Set Prescaler @ 256 } ////////////////////////////////// //Function : Config the usart baudrate //////////////////////////////////// void initUsart(uint16_t MY_UBBR) // NOTE: THE ARGUMENTS CANNOT BE EQUAL TO MACROS!!!! { //cli(); //Disable Global Interrupts to config USART UBRR0H = (unsigned char )(MY_UBBR>>8);//LOAD UPPER-8 BITS OF UBRR VALUE in register High UBRR0L = (unsigned char )MY_UBBR; //LOAD LOWER-8 BITS OF UBRR VALUE IN REGISTER LOW UCSR0B = (1<<TXEN0) | (1<<RXEN0); //ENABLE TX, RX AND DATA REGISTER EMPTY ENABLE INTERRUPT USART REGISTER 0 TO TRANSMIT AND RECEIVE DATA UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); //SETTING THE NUMBER OF DATA BITS (8) } ////////////////////////////////// //Function : send char to the usart //////////////////////////////////// void sendChar(uint8_t data) { while ( ( UCSR0A & (1<<UDRE0))==0 ); // DO NOTHING UNTIL UDR BECOME READY TO RECEIVE DATA UDR0 = data; //RECEIVING DATA FROM THE VARIABLE TO UDR0 AND SEND VIA USART TX } //////////////////////////////////// //Function : write string to the usart //////////////////////////////////// void sendString(char *data) { int len, i; len = strlen(data); //STORE THE ARRAY SIZE IN VARIABLE (LEN) for (i=0; i<len; i++) { // FOR CYCLE FROM BEGINNING OF CHAR ARRAY TILL THE END (LEN VALUE) if(data[i]=='\0') //CHECK EMPTY DATA INSIDE CHAR ARRAY { return; } else { sendChar(*(data+i)); //CALL FUNCTION AND SEND CHAR IN THAT POSITION } } } uint8_t read_char(void) { char buffer[50]={0}; //STARTING EMPTY ARRAY ={0} int i=0; for(i=0;i<49;i++) //RUNNING ARRAY POSITIONS { while ( !(UCSR0A & (1<<RXC0)) ); //WAIT TILL BUFFER BE READY TO RECEIVE DATA buffer[i] = UDR0; // READ STRING VIA RX, CHAR BY CHAR if (buffer[i]=='\b') //CHECK BACKSPACE KEY buffer[i]='\b'; //ERASE LAST VALUE if(buffer[i]=='\r') //CHECK ENTER KEY { sendString("\rO nome inserido foi :\r"); //CALL FUNCTION AND SEND STRING VIA TX sendString(buffer); //CALL FUNTION AND SEND THE "buffer" DATA return; } sendChar(buffer[i]); //PRINT CHAR BY CHAR "REAL TIME" AS IT'S PRESSED ON KEYBOARD } sendString("\rcaracteres a mais\r"); // PRINT STRING IF OVERCOME 49 ARRAY POSITIONS sendString(buffer); //PRINT ALL THE DATA IN THE "buffer" THAT HAS BEEN INSERTED (THE 50 CHARS) } /////////////////////////////////////// //FUNCTION : Convert to Int to Decimal /////////////////////////////////////// void write_dec(uint8_t b) { char number[10]; itoa(b,number,10); sendString(number); } /////////////////////////////////// //Function: Convert to Int to Hex /////////////////////////////////// void write_hex(uint8_t b) { char number[10]; itoa(b,number,16); sendString(number); } /////////////////////////////////// //Function: Convert to Int to Hex /////////////////////////////////// void write_bin(uint8_t b) { char a,number[8]={0}; int num=b,i; for(i=0;i<8;i++) { while(num!=0) { number[i]= num % 2; num = num / 2; } } for (i=8;i>0;i--) { a=number[i]; sendString(a); } //itoa(b,number,2); //sendString(number); } /////////////////////////////////// //Interrupt Call /////////////////////////////////// ISR(TIMER1_COMPA_vect) { b++; sendString("\r\n"); write_dec(b); sendString(" "); write_hex(b); sendString(" "); write_bin(b); sendString(" "); PORTB ^=(1<<PINB0);//Toggle PINB0 } //////////////////////////////////// //Function : main cycle //////////////////////////////////// int main(void) { char nome[] ="Nelson Macieira"; DDRB |= (1<<PINB0); // DEFINE PINB0 AS OUTPUT CLKPR = (1<<CLKPCE); // PREPARING PRESCALER TO EDITION CLKPR = (0); // EDITING PRESCALER TO 0, F_CPU=8000000/1 configTimer1(); initUsart(MYUBRRN); sei(); //Enable all activated interruptions while(1) { sendString("\r\n"); sendString(nome); sendString("\r\n"); sendString("Insira um nome\r\n"); read_char(); } }
Thanks for helping me :)
Regards, Nelson