Jump to content
43oh

help with code test error "printf" to msp430


Recommended Posts

Hi forum friends, first of all wanted to tell you that the forum is very helpful and pleasant.
I come to you because I am with a project with the launchpad msp430, and I would make the job easier to use the printf tool (), I have seen on this forum the courtesy opossum sharing code for printf, which is very good , but I wanted to get modify for use with UART communication but I could not, and I get errors in the image attached below, I also attached the printf code and test the code used.
Les agradesco the help, Greetings and excuse my translations.

 

140801010714155578.png

 

 

printf () code

#include "stdarg.h"

/**
 * Sends a single byte out through UART
 **/
void sendByte(unsigned char byte )
{
	while (!(IFG2&UCA0TXIFG));			// USCI_A0 TX buffer ready?
	UCA0TXBUF = byte;					// TX -> RXed character
}

/**
 * puts() is used by printf() to display or send a string.. This function
 *     determines where printf prints to. For this case it sends a string
 *     out over UART, another option could be to display the string on an
 *     LCD display.
 **/
void puts(char *s) {
	char c;

	// Loops through each character in string 's'
	while (c = *s++) {
		sendByte(c);
	}
}
/**
 * puts() is used by printf() to display or send a character. This function
 *     determines where printf prints to. For this case it sends a character
 *     out over UART.
 **/
void putc(unsigned  {
	sendByte(;
}
static const unsigned long dv[] = {
//  4294967296      // 32 bit unsigned max
		1000000000,// +0
		100000000, // +1
		10000000, // +2
		1000000, // +3
		100000, // +4
//       65535      // 16 bit unsigned max
		10000, // +5
		1000, // +6
		100, // +7
		10, // +8
		1, // +9
		};

static void xtoa(unsigned long x, const unsigned long *dp) {
	char c;
	unsigned long d;
	if (x) {
		while (x < *dp)
			++dp;
		do {
			d = *dp++;
			c = '0';
			while (x >= d)
				++c, x -= d;
			putc(c);
		} while (!(d & 1));
	} else
		putc('0');
}

static void puth(unsigned n) {
	static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F' };
	putc(hex[n & 15]);
}

void printf(char *format, ...)
{
	char c;
	int i;
	long n;

	va_list a;
	va_start(a, format);
	while(c = *format++) {
		if(c == '%') {
			switch(c = *format++) {
				case 's': // String
					puts(va_arg(a, char*));
					break;
				case 'c':// Char
					putc(va_arg(a, char));
				break;
				case 'i':// 16 bit Integer
				case 'u':// 16 bit Unsigned
					i = va_arg(a, int);
					if(c == 'i' && i < 0) i = -i, putc('-');
					xtoa((unsigned)i, dv + 5);
				break;
				case 'l':// 32 bit Long
				case 'n':// 32 bit uNsigned loNg
					n = va_arg(a, long);
					if(c == 'l' && n < 0) n = -n, putc('-');
					xtoa((unsigned long)n, dv);
				break;
				case 'x':// 16 bit heXadecimal
					i = va_arg(a, int);
					puth(i >> 12);
					puth(i >> 8);
					puth(i >> 4);
					puth(i);
				break;
				case 0: return;
				default: goto bad_fmt;
			}
		} else
			bad_fmt: putc(c);
	}
	va_end(a);
}

test code

#include <msp430.h>
#include "printf.c"

#define TXLED BIT0
#define RXLED BIT6
#define TXD BIT2
#define RXD BIT1

//const char string[] = { "Hello World\r\n" };
unsigned int i; //Counter

void _delay_ms(unsigned int ms){
	 int k;
	 for (k=ms;k>0;k--)
		 _delay_cycles(1000); // set for 16Mhz change it to 1000 for 1 Mhz
}
int tempOut(){
    int t=0;
    _delay_cycles(1000);              //wait 4 ref to settle
    ADC10CTL0 |= ENC + ADC10SC;      //enable conversion and start conversion
    while(ADC10CTL1 & BUSY);         //wait..i am converting..pum..pum..
    t=ADC10MEM;                       //store val in t
    ADC10CTL0&=~ENC;                     //disable adc conv
    return(int) ((t * 270682L - 18202115L) >> 16); //convert and pass
}


int main(void)
{
   WDTCTL = WDTPW + WDTHOLD; // Stop WDT
   DCOCTL = 0; // Select lowest DCOx and MODx settings
   BCSCTL1 = CALBC1_1MHZ; // Set DCO
   DCOCTL = CALDCO_1MHZ;

   P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
   P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
   P1DIR |= RXLED + TXLED;
   P1OUT &= 0x00;
   UCA0CTL1 |= UCSWRST;//reset uart

   UCA0BR0 = 104; // 1MHz 9600
   //UCA0BR0 = 0x08; // 1MHz 115200
   UCA0BR1 = 0; // 0x00 1MHz 115200
   UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
   //UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
   UCA0CTL1 = UCSSEL_2; // SMCLK ,**Initialize USCI state machine**
   //UC0IE |= UCA0RXIE; // Enable USCI_A0 RX interrupt
   //__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ int until Byte RXed

   ADC10CTL0=SREF_1 + REFON + ADC10ON + ADC10SHT_3 ; //1.5V ref,Ref on,64 clocks for sample
   ADC10CTL1=INCH_10+ ADC10DIV_3; //temp sensor is at 10 and clock/4
   _delay_ms(1);

   while (1)
   {
	   printf("Temp\r\n");
	   _delay_ms(200);

   }
}

P / D: Again thank you very much oPossum by the printf code

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...