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.

Help with EUSART on a 16f690

Status
Not open for further replies.

theredstealth

New Member
Working on a project for school and I hit my first road block.
Im using the the 690 as an ADC and then send out the data over the TX pin (RB7) to an SD card (openlog).

Everything functions but the data I am getting in the txt file on the sd is unreadable.

I attached the txt file and my code. The code is a mess as I commented bits out to try new stuff instead of deleting.

Am I missing something? I have spent hours pouring through the internet and datasheets and I am more confused than when I started.

Thanks in advance
 
That's because SD cards use an SPI interface. You need to use the SSP port on the F690, not the UART.

You would connect the SD card as follows -

RC7/SDO ---> Pin 2 / Data In
RB6/SCK ---> Pin 5/SCLK
RB4/SDI ---> Pin 7/ Data Out

You also need to designate a port pin on the F690 to drive the Card Select/Pin 1 pin on the SD card.

Then you would set up the F690 in SPI master mode. Consult the data sheet of the F690 under the section "SSP Module Overview" to learn how to do this.
 
Last edited:
First, I suggest you tidy and indent your code. I find,
Code:
void Decide(void)
{
//Assign the upper 4 bits of ADRESH to the lower 4 bits
//of LED_Output
LED_Output = ADRESH >> 4; //Shifts the bits in ADRESL 4 bits
//to the right
}
very hard to read.

I'd rather see,
Code:
void Decide(void)
{
	//Assign the upper 4 bits of ADRESH to the lower 4 bits of LED_Output
	LED_Output = ADRESH >> 4; //Shifts the bits in ADRESL 4 bits to the right
}
However, for such a simple routine comments aren't required. Especially ones that repeat what the code does.

What I also suggest is trying something like,
Code:
void main(void){
    Initialize(); 	//Initialize the relevant registers
    for(i=0,i<10,i++){
        printf(i+0x30);	//send ascii "0" to "9"
    }
    while(1);		//don't allow main to exit
}
and see if the file contains 0123456789.

Also, what exactly are you trying to log?

Mike.
 
That's because SD cards use an SPI interface. You need to use the SSP port on the F690, not the UART.

You would connect the SD card as follows -

RC7/SDO ---> Pin 2 / Data In
RB6/SCK ---> Pin 5/SCLK
RB4/SDI ---> Pin 7/ Data Out

You also need to designate a port pin on the F690 to drive the Card Select/Pin 1 pin on the SD card.

Then you would set up the F690 in SPI master mode. Consult the data sheet of the F690 under the section "SSP Module Overview" to learn how to do this.

Hi Jon,

It's not just an SD card. It's one of these.

Mike.
 
sprintf() is available (at a memory cost) , You create a file buffer and use the function to print to it... you the send that buffer serially to the logger.

Code:
char fileBuffer[20];
int acdVal;

sprintf(fileBuffer,"%d\n\r", adcVal);

// You now have a string called fileBuffer that contains the text you need.
 
This sends it out as a hex one byte at a time works ok But i'm not the best at Hi-tech C been playing

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(ADRESL);	// return 8 LSB 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 SendUART(unsigned char dataValue){
		// output one byte  
	 while(!TXIF)	// set when register is empty 
		continue;
         TXREG = dataValue; //Start transmission
}


void main(void){
	unsigned char x; 
	init_a2d();	// initialise the A2D module
	GIE=0;		// we don't want interrupts
    SetupUART();
	
	for(;;){
 		x=read_a2d(1);		// sample the analog value on RA0	
               SendUART(x);
	       __delay_ms(1);
		SendUART(10);      // and end of line and returns 
		__delay_ms(1);
		SendUART(13);
		__delay_ms(1000);  // delay
	}
}


View attachment 61021
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top