Go Down

Topic: Newbie trying to migrate C code to Arduino code. (Read 621 times) previous topic - next topic


Ok, i'm a total newbie. I get simple electrical ideas and very heavily commented simple code. :) I mainly think in physical things if that helps at all.

I'm trying to get this code:

which makes almost no sense to me and is for for this project:


Working with a duemilanove and an EL LCD that I have on hand.

I understand it's a timer which is switching on and off 2 pins at 150Hz. But I don't understand how to make 2 different timers at the same time. I also don't really know what parts of that AVR code are important and what are excess that the Arduino doesn't need at all.

Any info would be helpful, thank you. :)


I'm trying to get this code:
Working with a duemilanove and an EL LCD that I have on hand.

The code uses the internal HW timers of the CPU, and so it isn't really convertible to the arduino-style sketch (where the HW timers are used to provide PWM for analogWrite, and normal programmers aren't expected to touch the timers.  It should still WORK within a sketch, assuming you don't need the timers for something else.

You'd probably want to replace the uart and lcd code with the standard arduino stuff.
Stripping the LCD code and replacing the uart with arduino serial calls, I get the following, which compiles and seems to work...

Code: [Select]
// based on backlight.c
// from NerdKits by mrobbins@mit.edu
// rewritten for (mostly) arduino environment
// LCD code removed; this manipulates the backlight only

// PB1 (D9) -- OC1A -- backlight high-speed switchy thing (nFET gate)
//            used to generate the high voltage supply
// PB3 (D11) -- OC2A -- backlight low-speed switchy thing (nFET gate)
//            used to pulse the AC to the electroluminescent

void setup() {
 // PB1,3 as output
 DDRB |= (1<<PB1) | (1<<PB2) | (1<<PB3);

 // Timer1: Toggle CTC, 10-bit, non-inverting, clocked from CLK/1
 TCCR1A = (1<<COM1A0);
 TCCR1B = (1<<WGM12) | (1<<CS10);
 OCR1A = 51; // 141 kHz, or it would be a 14.x MHz

 // Timer2: Toggle CTC, clocked from CLK/1024
 TCCR2A = (1<<COM2A0) | (1<<WGM21);
 TCCR2B = (1<<CS22) | (1<<CS21) | (1<<CS20);
 OCR2A = 40; // 175 Hz

 // init serial port

void loop()
 double freq_a, freq_b;
 char x;

 // allow user to adjust timings via serial port
 if(Serial.available()) {
   x = Serial.read();
   if(x == 'w') {
     OCR1A += 1;
   else if(x=='q') {
     OCR1A -= 1;
   else if(x=='s') {
     OCR2A += 1;
   else if(x=='a') {
     OCR2A -= 1;

   // convert to human-readable frequencies
   freq_a = F_CPU;
   freq_a = freq_a / 2.0 / (OCR1A+1.0);
   freq_b = F_CPU;
   freq_b = freq_b / 2.0 / 1024.0 / (OCR2A+1.0);
   Serial.print(", ");
   Serial.print("         |     OCR2A=");
   Serial.print(", ");
 } // Serial.available


Wow, thank you for putting the details in words I understand, that's incredibly helpful :)

Hrrm, what would you suggest if I did need the timer (Which i'm assuming is "void loop") for something else?


Go Up