Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

printf

2005-03-24 by neptunus1000

Hello,

I'wondering why my classes are not working. I want to build a Com 
class so a can use printf to put something on the uart.

The functie's com.UARTWriteChar('x'); com.__putchar('\n'); in the 
main are working good. But the printf is not working. Can some one 
help my.

[code]
//Main.cpp
#include <targets/LPC210x.h>
#include <stdio.h>
#include "Com.h"

int main(void){
  Com com;
  com.UARTInitialize(9600);
  com.delay(1000);
  com.UARTWriteChar('x');
  com.__putchar('\n');
  com.printf("test");
  return 0;
}

//Com.h
#include <targets/LPC210x.h>
#include <stdio.h>

class Com{
  private:
    int OSCILLATOR_CLOCK_FREQUENCY;
  public:
    Com(void);
    ~Com(void);
    unsigned int processorClockFrequency(void);
    unsigned int peripheralClockFrequency(void);
    void delay(int);
    void UARTInitialize(unsigned int);
    void UARTWriteChar(unsigned char);
    unsigned char UARTReadChar(void);
    void __putchar(int);
    void printf(const char *);
};

Com::Com(void){
  OSCILLATOR_CLOCK_FREQUENCY = 14745600;
}

Com::~Com(void){

}

unsigned int Com::processorClockFrequency(void){
  return OSCILLATOR_CLOCK_FREQUENCY * (PLLCON & 1 ? (PLLCFG & 0xF) + 
1 : 1);
}

unsigned int Com::peripheralClockFrequency(void){
  unsigned int divider;
  switch (VPBDIV & 3)
    {
      case 0:
        divider = 4;
        break;
      case 1:
        divider = 1;
        break;
      case 2:
        divider = 2;
        break;
    }
  return processorClockFrequency() / divider;
}

void Com::delay(int n){
  volatile int i;
  for (i = 0; i < n; ++i);
}

void Com::UARTInitialize(unsigned int baud){
  unsigned int divisor = peripheralClockFrequency() / (16 * baud);
  U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
  U0DLL = divisor & 0xFF;
  U0DLM = (divisor >> 8) & 0xFF;
  U0LCR &= ~0x80; /* Disable DLAB */
  PINSEL0 = PINSEL0 & ~0xF | 0x5;
  U0FCR = 1;
}

void Com::UARTWriteChar(unsigned char ch){
  while ((U0LSR & 0x20) == 0);
  U0THR = ch;
}

unsigned char Com::UARTReadChar(void){
  while ((U0LSR & 0x01) == 0);
  return U0RBR;
}

void Com::__putchar(int ch){
  if (ch == '\n')
    UARTWriteChar('\r');
  UARTWriteChar(ch);
}

void Com::printf(const char *string){
  printf(string);
}
[/code]

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.