Hi
so I wrote some function ( or better say copied) for USART
I want to design a function that i pass to it a string or an array via pointers and it send it by usart
first .. my code :
/* * Learn.c * * Created: 3/15/2019 7:07:23 PM * Author : AbDoO_ */ #define F_CPU 1000000UL #include <avr/io.h> #include <util/delay.h> //ADC Vars : unsigned int sensor_value,adh,adl; //Conversion Vars : unsigned char txt[6]; //USART Functions : void Usart_Init(unsigned int USART_BAUDRATE); void Usart_Write(unsigned char Data2Write); unsigned char Usart_Read(); void int2str(unsigned int d); int main(void) { DDRB =0xff; DDRC =0x00; Usart_Init(4800); char x =0; ADCSRA |= (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // SET ADC PRESCALAR TO 8 - 125KHZ SAMPLE RATE @ 1MHZ ADMUX |= (1 << REFS0); // SET ADC REFERENCE TO AVCC //ADMUX |= (1 << ADLAR); // LEFT ADJUST ADC RESULT TO ALLOW EASY 8 BIT READING // NO MUX VALUES NEEDED TO BE CHANGED TO USE ADC0 ADCSRA |= (1 << ADFR); // SET ADC TO FREE-RUNNING MODE ADCSRA |= (1 << ADEN); // ENABLE ADC ADCSRA |= (1 << ADSC); // START A2D CONVERSIONS while (1) { _delay_ms(100); adl= ADCL; adh = ADCH; sensor_value= (adh<<8) | adl ; int2str(sensor_value); // the code section i want to replace with a function x=0; while(txt[x] != 0 ){ Usart_Write(txt[x]); x++; } Usart_Write(0x0d); } } void int2str(unsigned int d) { txt[0] = (d/10000) + 48 ; d = d - (d/10000)*10000; txt[1] = (d/1000) + 48 ; d = d - (d/1000)*1000; txt[2] = (d/100) + 48 ; d = d - (d/100)*100; txt[3] = (d/10) + 48 ; d = d - (d/10)*10; txt[4] = (d/1) + 48 ; d = d - (d/1)*1; txt[5] = 0; } void Usart_Init(unsigned int USART_BAUDRATE) { unsigned int BAUD_PRESCALE; BAUD_PRESCALE = (((F_CPU / (USART_BAUDRATE * 16UL))) - 1); UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register }; void Usart_Write(unsigned char Data2Write) { while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it UDR = Data2Write; }; unsigned char Usart_Read() { while((UCSRA & (1<<RXC)) == 0){}; return UDR ; };
the int2str() is for transferring the int value of the adc to a string so i can send it by usart
Every thing works 100 percent accept(but not) for some warnings that i ignored like :
"" Warning array subscript has type 'char' [-Wchar-subscripts] ""
but the code works perfectly
i want another function to do this part of the code
x=0; // the code section i want to replace with a function while(txt[x] != 0 ){ // considering that every string sentence ends with a NULL or a 0 Usart_Write(txt[x]); x++; } Usart_Write(0x0d); // add a line
But instead of the array "txt" i want a pointer so i can pass any array or string i want
i tried some thing like this
void Usart_Write_Text(unsigned int* p){ // i tried unsigned char also char x=0; // the code section i want to replace with a function while(p[x] != 0 ){ // I also tried useing *p instead of p Usart_Write(p[x]);// I also tried using *p instead of p x++; } Usart_Write(0x0d); }
Please you are free to copy any thing ,
please modify my Usart_Write_Text(..) function and make it work
NOTE:
i want to use it somehow like this :
Usart_Write_Text("HI test1 test 2 ");
Usart_Write_Text(txt); // or &txt you tell me
(SIDE QUESTION): is this considered a bad or a good question?( i mean forum) (so i can improve how i ask in the futuer )
THANKS ALOT