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: