0% found this document useful (0 votes)
11 views3 pages

Power SWR Meter Atmega8

This document contains an AVR C program for measuring forward and reflected power using an ADC, calculating the Standing Wave Ratio (SWR), and displaying the results on an LCD. It includes functions for reading ADC values, averaging them, calculating power and SWR, and formatting the output for display. The program also features a vertical bar graph representation of the power levels on the LCD.

Uploaded by

arielect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Power SWR Meter Atmega8

This document contains an AVR C program for measuring forward and reflected power using an ADC, calculating the Standing Wave Ratio (SWR), and displaying the results on an LCD. It includes functions for reading ADC values, averaging them, calculating power and SWR, and formatting the output for display. The program also features a vertical bar graph representation of the power levels on the LCD.

Uploaded by

arielect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <avr/io.

h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd1602.h"

#define MAX_FWD 200.0


#define MAX_REF 40.0
#define R_LOAD 50.0
#define AVG_COUNT 10

// Custom karakter untuk bargraph vertikal 7 bagian


uint8_t bar_char[8][8] = {
{0,0,0,0,0,0,0,0}, // Kosong
{0,0,0,0,0,0,0,31}, // 1/7
{0,0,0,0,0,0,31,31}, // 2/7
{0,0,0,0,0,31,31,31},// 3/7
{0,0,0,0,31,31,31,31},// 4/7
{0,0,0,31,31,31,31,31},// 5/7
{0,0,31,31,31,31,31,31},// 6/7
{0,31,31,31,31,31,31,31}// Full 7/7
};

// Baca ADC (channel 0 untuk FWD, 1 untuk REF)


uint16_t readADC(uint8_t ch) {
ADMUX = (1 << REFS0) | (ch & 0x07);
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
return ADC;
}

// Rata-rata pembacaan ADC


uint16_t readADC_avg(uint8_t ch) {
uint32_t sum = 0;
for (uint8_t i = 0; i < AVG_COUNT; i++) {
sum += readADC(ch);
_delay_ms(2);
}
return sum / AVG_COUNT;
}

// Konversi tegangan ke daya


float calcPower(uint16_t adc) {
float v = adc * (5.0 / 1023.0);
return (v * v) / R_LOAD;
}

// Hitung SWR dari FWD & REF


float calcSWR(float fwd, float ref) {
if (ref >= fwd) return 99.99;
float VSWR = (fwd + ref) / (fwd - ref);
return VSWR;
}

// Konversi daya ke level bargraph (0 - 7)


uint8_t powerToBar(float power, float maxPower) {
uint8_t bar = (power / maxPower) * 7;
if (bar > 7) bar = 7;
return bar;
}
// Format angka daya dengan digit sesuai
void formatPower(float p, char *buf) {
if (p < 10.0)
dtostrf(p, 5, 2, buf);
else if (p < 100.0)
dtostrf(p, 5, 1, buf);
else
dtostrf(p, 5, 0, buf);
}

// Format SWR
void formatSWR(float swr, char *buf) {
if (swr >= 10.0)
dtostrf(swr, 4, 1, buf);
else
dtostrf(swr, 4, 2, buf);
}

// Gambar bargraph vertikal di kolom tertentu


void drawVerticalBar(uint8_t col, uint8_t level) {
lcd_gotoxy(col, 0); lcd_putc(level > 3 ? bar_char[level][7] : ' ');
lcd_gotoxy(col, 1); lcd_putc(bar_char[level][7]);
}

int main(void) {
lcd_init();
lcd_clear();

// Load karakter custom untuk bargraph vertikal


for (uint8_t i = 1; i <= 7; i++) {
lcd_create_char(i, bar_char[i]);
}

// Inisialisasi ADC
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1);

char buf[10];

while (1) {
uint16_t adcFWD = readADC_avg(0);
uint16_t adcREF = readADC_avg(1);

float fwd = calcPower(adcFWD);


float ref = calcPower(adcREF);
float swr = calcSWR(fwd, ref);

// Hitung level bargraph untuk FWD


uint8_t barFWD = powerToBar(fwd, MAX_FWD);

// Tampilkan FWD
lcd_gotoxy(0, 0);
lcd_puts("FWD:");
formatPower(fwd, buf);
lcd_puts(buf);

// Gambar bargraph vertikal (kolom 10 - 16)


for (uint8_t i = 0; i < 7; i++) {
drawVerticalBar(10 + i, (barFWD > i) ? barFWD : 0);
}

// Tampilkan REF & SWR


lcd_gotoxy(0, 1);
lcd_puts("REF:");
formatPower(ref, buf);
lcd_puts(buf);

lcd_gotoxy(9, 1);
lcd_puts("SWR:");
formatSWR(swr, buf);
lcd_puts(buf);

_delay_ms(300);
}
}

You might also like