Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need a little help with printf

Status
Not open for further replies.

be80be

Well-Known Member
I'm wanting to send decimal to ascii string but I can't get it to output a string

Code:
#include <stdio.h>
#include <htc.h>
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & 
			CP_OFF & BOREN_OFF & CPD_OFF & IESO_OFF & FCMEN_OFF);
#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
 #define _XTAL_FREQ 8000000
#endif


void init_a2d(void){
        OSCCON = 0B01111111;
	TRISA =  0B00000001;
	ADCON0 = 0B10000001;
	ADCON1 = 0B01110000;
	ANSEL =  0B00000000;
	ANSELH = 0B00000000;
}

unsigned char read_a2d(unsigned char channel){
	channel=0x01;	
	ANSEL=(channel);	
        GO_DONE	=1;	// initiate conversion on the selected channel
	while(GO_DONE)continue;
	return((ADRESH<<8)+(ADRESL));	// return 10bits of the result
}
void SetupUART(void){
     SPBRGH = 0;
     SPBRG = 51; //9600 bps
     TXSTA = 0x24; //8-bit, Transmission enabled, High Speed
     RCSTA = 0x80; //Disable Reception, Enable UART module
     BAUDCTL = 0; //8-bit, Auto Baud disabled
}

void putch(unsigned char byte) {
		// output one byte //
	 while(!TXIF)	// set when register is empty 
		continue;
             TXREG = byte; //Start transmission
}
unsigned char getch() {
	return	read_a2d(1);
}

unsigned char getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}

void main(void){
	unsigned char x; 
	init_a2d();	// initialise the A2D module
	GIE=0;		// we don't want interrupts
        SetupUART();
	
	while(1){
		x = getch();	// returns adc reading
		printf("\rI detected [%c]",x);	// sends out x 
    	        __delay_ms(1000);  // delay
 	         printf("Unsigned value: %u\n", 150);	// this is just for seeing how printf works 
	}
}
 
Last edited:
Hi Burt.

I think I read somewhere that you need to write a function for that. Since there are decimal digits in ascii, you have to write a function to take the decimal input, add the certain value to it (thinking it's 48, decimal) to bring it into the digit section of the ascii table, then return that value to the printf. Not sure if this makes much sense. I'll see if I can find an example for you.

Regards,
Der Strom

[EDIT] Okay, I found the following:

**broken link removed**

I'm not sure if it's exactly what you're looking for, but it might help. It's a little different from what I described above :p
 
Last edited:
Sprintf and printf I think I have them figured. problem now is My adc value needs to be unsigned short to hold the 10 bits
Usart can only send a byte at a time if I send a short to the buffer I get a big error but if I send a char it shows up right on realterm

I changed this part it works if you just want 8bit ADC reading
Code:
	while(1){
		x = getch();	// read a response ADC
    	printf("I detected : %d\n", x);	// sends it out
    	__delay_ms(1000);  // delay	
	}
}
 
Last edited:
I'll give that a try I read where you need to put it in array like char(2) and shift it in

But printf can handle conversions

Sure not like swordfish all you do is USART.Write("Decimal : ", DecToStr(ADVal), 13, 10)
 
Status
Not open for further replies.

Latest threads

Back
Top