Digital soldering station project need help

Tolik

Member
2007-03-24 6:31 am
Hi,
I`m building digital soldering station based on atmega 8 chip.
The code was redy, but I wont to use rotary encoder to control temperature. Now the problem that I can`t to write an interface routine. I tried some trics but It still not working.
If somebody familiar with C & AVR stuff help me please.
Encoder connected to PINC1,PINB4 & to ground, MCU pins set to 1.

here is the the part of code:

/*****************************************************
Chip type : ATmega8
Program type : Application
Clock frequency : 8,0000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/

#include <mega8.h>
#include <stdio.h>
#include <math.h>
#include <delay.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>

#define ADC_VREF_TYPE 0xC0

#define KEY1_DOWN (encoder == 2)
#define ENC_A (PINC.1 == 0)
#define KEY2_DOWN (PINC.2 == 0)
#define KEY3_DOWN (PINC.3 == 0)
#define KEY4_DOWN (PINB.5 == 0)
#define KEY5_DOWN (encoder == 0)
#define ENC_B (PINB.4 == 0)
#define KEY6_DOWN (PINB.3 == 0)

#define BEEP PORTB.0

//unsigned char KEY1_DOWN;
//unsigned char KEY5_DOWN;
unsigned char lcd_buffer1[17] = " ";
unsigned char lcd_buffer2[17] = " ";
unsigned char lcd_buffer12[4] = " ";
unsigned char lcd_buffer21[5] = " ";

/* [0] " Heating..." */
flash unsigned char Mes0[]={23,23,23,72,101,97,116,105,110,103,46,46,46,23,23,0};

/* [1] " REDY :)" */
flash unsigned char Mes1[]={23,23,23,23,23,23,82,69,68,89,23,23,23,23,23,0};

/* [2] "Setup" */
flash unsigned char Mes2[]={23,23,23,23,83,101,116,117,112,23,23,0};

/* [3] "Temperature:" */
flash unsigned char Mes3[]={23,84,101,109,112,101,114,97,116,117,114,101,23,23,23,0};


volatile unsigned int adc_data = 0, T, ee_tmprSet = 0, T_disp = 0;
volatile int ReadKey = 0, KeyDelay = 0, Mode = 0, program = 1;

unsigned int i=1, T0 = 3, Kp = 70; //

volatile int pwm_val = 0; // store PWM data in 1/1024


eeprom unsigned int T_prog[3] = { 150, 250, 400 }; // themperature presets in EEPROM
unsigned int T_set[3];

static void avr_init();
void green(void);
void red(void);
void my_beep(void);
unsigned int read_adc(unsigned char adc_input);

// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
// Read the AD conversion result
adc_data=ADCW;
}

// Read the AD conversion result
// with noise canceling
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
#asm
in r30,mcucr
cbr r30,__sm_mask
sbr r30,__se_bit | __sm_adc_noise_red
out mcucr,r30
sleep
cbr r30,__se_bit
out mcucr,r30
#endasm
return adc_data;
}

signed char
encoder(void)
{
static unsigned char a, b;
if (!a & !b) {
if (ENC_A) {
a = ENC_A;
return (2);
}
if (ENC_B) {
b = ENC_B;
return (1);
}
}
if (a & !b) {
if (!ENC_A) {
a = ENC_A;
return (1);
}
if (ENC_B) {
b = ENC_B;
return (2);
}
}
if (a & b) {
if (!ENC_A) {
a = ENC_A;
return (2);
}
if (!ENC_B) {
b = ENC_B;
return (1);
}
}
if (!a & b) {
if (ENC_A) {
a = ENC_A;
return (1);
}
if (!ENC_B) {
b = ENC_B;
return (2);
}
}
return (0);
}


// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=0x00;
if (ReadKey == 0) {
if (KeyDelay == 0) { // If KeyDelay == 0, possible to repeat
KeyDelay = 0;

if (KEY1_DOWN) {
ReadKey = 1;
my_beep();
}
if (KEY2_DOWN) {
ReadKey = 2;
my_beep();
}
if (KEY3_DOWN) {

ReadKey = 3;
my_beep();
}
if (KEY4_DOWN) {
ReadKey = 4;
my_beep();
}
if (KEY5_DOWN) {
ReadKey = 5;
my_beep();
}
if (KEY6_DOWN) {

ReadKey = 6;
my_beep();
}

if (ReadKey) {
KeyDelay = 10;
}
} else {
KeyDelay--;
}
} // if
i--;

if (i==0) {
T_disp = T; // display temp calculation !!!!!!!!!!!!
i=15;
}
}

void main(void)
{
avr_init();

while (1)
{
adc_data=read_adc(0);
T = adc_data / 2 ; // ADC conversion setup !!!!!!!!!!!!!!!!

pwm_val = Kp * (ee_tmprSet - T + T0);

if (pwm_val > 1023) pwm_val = 1023;
if (pwm_val < 0) pwm_val = 0;

if ((T > (ee_tmprSet - 6)) && (T < (ee_tmprSet + 6))) {
green();
} else {
red();
}

if (ReadKey == 5)
{
if (Mode == 1 || Mode == 2 || Mode == 3) {
T_set[Mode - 1] = T_set[Mode - 1] + 1;
// } else {
}
if (ee_tmprSet >= 420){ // themperature limit max.
ee_tmprSet = ee_tmprSet + 0; // themperature step +
} else {
ee_tmprSet = ee_tmprSet + 5;
}
ReadKey = 0;
}
if (ReadKey == 1)
{
if (Mode == 1 || Mode == 2 || Mode == 3) {
T_set[Mode - 1] = T_set[Mode - 1] - 1;
//} else {
}
if (ee_tmprSet <= 0){
ee_tmprSet = ee_tmprSet - 0; // // temperature step -
}else{
ee_tmprSet = ee_tmprSet - 5;
}
ReadKey = 0;
}
if (ReadKey == 3)
{
lcd_clear();
Mode++;
if (Mode == 4) {
#asm("cli")
T_prog[0] = T_set[0];
T_prog[1] = T_set[1];
T_prog[2] = T_set[2];
#asm("sei")
Mode = 0;
}
ReadKey = 0;
}

if (Mode == 1 || Mode == 2 || Mode == 3)
{
sprintf(lcd_buffer1, Mes2);
sprintf(lcd_buffer12, "P%i:", Mode);
sprintf(lcd_buffer2, Mes3);
sprintf(lcd_buffer21, "%03i", T_set[Mode - 1]);

lcd_gotoxy(12, 0);
lcd_puts(lcd_buffer12);
lcd_gotoxy(13, 1);
lcd_puts(lcd_buffer21);
}


if (ReadKey == 6)
{
ee_tmprSet = T_set[0];
program = 1;
ReadKey = 0;
}
if (ReadKey == 4)
{
ee_tmprSet = T_set[1];
program = 2;
ReadKey = 0;
}
if (ReadKey == 2)
{
ee_tmprSet = T_set[2];
program = 3;
ReadKey = 0;
}

if (Mode == 0)
{

sprintf(lcd_buffer1, "Tc=%03i Ts=%03i P%i", T_disp, ee_tmprSet, program);


if (T_disp >= ee_tmprSet)
{

sprintf(lcd_buffer2, Mes1);

}
else
{
sprintf(lcd_buffer2, Mes0);
}

}




lcd_gotoxy(0, 0);
lcd_puts(lcd_buffer1);
lcd_gotoxy(0, 1);
lcd_puts(lcd_buffer2);



OCR1AH = (unsigned char)(pwm_val>>8);
OCR1AL = (unsigned char)pwm_val;

};
}

void green(void) {
PORTC.4 = 0;
PORTC.5 = 1;
}

void red(void) {
PORTC.4 = 1;
PORTC.5 = 0;
}
void my_beep(void) {
BEEP = 1;
delay_ms(25); // beep lenth -------------------------
BEEP = 0;
}