Need help Converting EL Backlight PS atmega168 code for Arduino UNO has an EL Backlight power supply project that has code for an ATmega168 but won’t compile
for my UNO.
NerdKits - LCD Backlight - Electroluminescent Inverter

I was told that it should be compatible with a 328 so I don’t know what the problem is.
I’ve only been programming C++ (arduino only) for a couple of months so it’s probably something obvious
to everyone else. I changed the obvious (even to me) stuff like the controller pin designations and then if
gave an error for " uart_init();"

Here’s the code I got from the above website with minor changes for the cpu pins:

 // backlight.c
// for NerdKits with ATmega168

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PB1 -- OC1A -- backlight high-speed switchy thing (nFET gate)
//		used to generate the high voltage supply
// PB3 -- OC2A -- backlight low-speed switchy thing (nFET gate)
//		used to pulse the AC to the electroluminescent
const int PB1=1;
const int PB2=2;
const int PB3=3;

int main() {
  // 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
  // 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
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  char x;
  //			      123456789012345678901234
  const char *lineone = PSTR(" NerdKits LCD Backlight ");
  const char *linetwo = PSTR("");
  // fire up the LCD

  // write lines

  double freq_a, freq_b;
  // main loop
  while(1) {
    // allow user to adjust timings via serial port
    if(uart_char_is_waiting()) {
      x = uart_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);
      printf_P(PSTR("OCR1A=%3u, %.0f Hz\t\t|\tOCR2A=%3u, %.2f Hz\r\n"), OCR1A, freq_a, OCR2A, freq_b);

  return 0;

I have the Atmel ATmega328 datasheet and while I don’t know how to use the timers I can look up information
or read it if I know where to look.
Does anyone know why I’m getting a uart_init error ?

I’m an Electronics Engineering Tech and I know it’s easy to generate the 150kHz and 150hz frequencies needed
to drive the mosfets of this PS using 555 timers but the reason I’m using Arduino’s is to upgrade my job skill set
so I can design and build uController based test setups or do data acquisition so using 555s would sort be
defeating the purpose of my objective. If anyone can help me get this program working I have an Oscilloscope
I can use to look at the two frequencies. I have 3 Arduino UNOs, 2 Pro-Mini’s , 2 ATtiny85s and two
ATmega328P chips with OPTIBOOT Bootloader. Only one of them is breadboarded right now. I also have
a 4-bit 1602 LCD, an I2C LCD, a 6-Digit, 7-Segment LCD, 4 DS18B20s, 6, 4 MCP4162-103/P digital pots, and a
couple of TLC5940NTs . I just completed a 12-key membrane Keypad project using a 74c922 16 key encoder
ic. (yeah, why did I use that ? (I ordered the wrong keypad when I ordered the ic) so the program required
some modificaiton;

Most of the code was from GrayNomad because he couldn’t stand to look at my original code

Any help would be appreciated.
I’m not asking you to write the program for me, just give me some pointers and point me in the
right direction (like where to find the timer stuff in the data sheet (Chapters 15 -18 are the Timers, and
20 & 21 are USART), but I don’t know how to set up the Timers. I found some C code examples on
page 117 but I couldn’t find anything that looks like this part of the inverter program:

 // Timer1: Toggle CTC, 10-bit, non-inverting, clocked from CLK/1
  TCCR1A = (1<<COM1A0);
  TCCR1B = (1<<WGM12) | (1<<CS10);
  OCR1A = 51; // 141 kHz
  // Timer2: Toggle CTC, clocked from CLK/1024
  TCCR2A = (1<<COM2A0) | (1<<WGM21);
  TCCR2B = (1<<CS22) | (1<<CS21) | (1<<CS20);
  OCR2A = 40; // 175 Hz

From what I could find in the Reference section of the Arduino website the above looks like
some bit shifting:

Bitwise Operators

& (bitwise and)
| (bitwise or)
^ (bitwise xor)
~ (bitwise not)
<< (bitshift left)
>> (bitshift right)

Thanks in advance for your help.

All the instructions ARE bits manipulation. Used to set or clear a single bit in the MCU registers.
TCCR1A, OCR1A, … are all registers of the MCU.
You need also check in datasheet of 328 and 168 if this registers are present in both mcu and if bits are are used in the same manner.

DDRB |= (1<<PB1) | (1<<PB2) | (1<<PB3);

This is the register that control the “direction” setting of pins. Physical pin of MCU.

A tutorial about timers:

Thanks. Can you translate into plain english (describe with words) one of the bit manipulation instructions you referred to?

I can try but my English is terrible.

DDRB |= (1<<PB1) | (1<<PB2) | (1<<PB3);

DDRB is the register that control the B port and is 8 bits (so it control 8 pins). (Chapter 13 of 328 datasheet)
In C you can write
val+=3 is the same val=val+3
DDRB |= value. is the same DDRB=DDRB | value
The | is the or operator (operation on bits, not logical or compare )
In this case you can think the | operator as a sum bit to bit

<< operator is the shift bits to left. You set to 1 the corresponding bit of PBx
The instruction set to 1 the bit of PB1, PB2 and PB3.
You need to see this pin out diagram (by xxxPighi)

to know the physical pin of MCU → Arduino Uno board pin

A good tutorial about direct port manipulation:

and here (but not complete):

Thank you gentleman. I will peruse the tutoriials straightaway!