/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 02/24/2016
Author : Mohammad Nouri
Company :
Comments:
Chip type : ATmega8
Program type : Application
AVR Core Clock frequency: 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
#include <stdio.h>
// Alphanumeric LCD functions
#include <alcd.h>
/****************************************************/
#define start_key_1 PINB.0
#define start_key_2 PINB.1
#define tctrl_evout PINB.2
#define mgnt_sensor PINB.3
#define jack PORTD.0
#define motor PORTD.1
#define led_error PORTD.7
#define led_live PORTD.6
/****************************************************/
// Declare your global variables here
bit error = 0;
unsigned int cntr = 0; // timer devider
unsigned int debounce = 0; // debounce counter
signed int press_time = 0; // press time
signed int feed_time = 0; // feed time
char buf[];
/****************************************************/
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
// 10 ms
TCNT0 = 0x64;
cntr = cntr + 10;
if (cntr == 500)
{
led_live = ~led_live;
cntr = 0;
}
}
/****************************************************/
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
// 50 ms
TCNT1H = 0x0B;
TCNT1L = 0xDC;
error = led_error = 1;
}
/****************************************************/
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
/****************************************************/
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=P State2=P State1=P State0=P
PORTB=0x0F;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State7=0 State6=0 State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xC3;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
TCCR0=0x04;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0B;
TCNT1H=0xDC;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x82;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
/*--------------------------------------------------*/
// only for test...
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 2
// RD - PORTD Bit 3
// EN - PORTD Bit 4
// D4 - PORTC Bit 2
// D5 - PORTC Bit 3
// D6 - PORTC Bit 4
// D7 - PORTC Bit 5
// Characters/line: 16
lcd_init(16);
/*--------------------------------------------------*/
// Global enable interrupts
#asm("sei")
// start time! hardware error!
while (start_key_1 == 0 || start_key_2 == 0 || tctrl_evout == 1 || mgnt_sensor == 0)
{
led_error = 1;
}
/*--------------------------------------------------*/
while (1)
{
// Place your code here
while (start_key_1 == 0)
{
debounce++;
if (debounce > 5000)
{
debounce = 0;
if (error == 0)
{
while (start_key_1 == 0 && start_key_2 == 0)
{
debounce++;
if (debounce > 5000) // 5000 * 250ns = 1.25 ms
{
debounce = 0;
jack = 1;
motor = 0;
if (mgnt_sensor == 0) // JACK : DOWN , LED : ON
{
// wait
delay_ms(press_time);
//
jack = 0;
// delay_ms(100.. replaced by while..
delay_ms(2000);
while (start_key_1 == 0 || start_key_2 == 0 || tctrl_evout == 1 || mgnt_sensor == 0);
motor = 1;
// waite
delay_ms(feed_time);
//
motor = 0;
}
}
}
debounce = 0;
jack = 0;
motor = 0;
}
else
{
// wait for Ok..
while (start_key_1 == 0 || start_key_2 == 0 || tctrl_evout == 1 || mgnt_sensor == 0);
error = led_error = 0;
// timer seting ~ 50 ms
TCNT1H = 0x0B;
TCNT1L = 0xDC;
}
}
}
/*--------------------------------------------------*/
while (start_key_2 == 0)
{
debounce++;
if (debounce > 5000) // 5000 * 250ns = 1.25 ms
{
debounce = 0;
if (error == 0)
{
while (start_key_1 == 0 && start_key_2 == 0)
{
debounce++;
if (debounce > 5000) // 5000 * 250ns = 1.25 ms
{
debounce = 0;
jack = 1;
motor = 0;
if (mgnt_sensor == 0) // JACK : DOWN , LED : ON
{
// waite
delay_ms(press_time);
//
jack = 0;
// delay_ms(100.. replaced by while..
delay_ms(2000);
while (start_key_1 == 0 || start_key_2 == 0 || tctrl_evout == 1 || mgnt_sensor == 0);
motor = 1;
// waite
delay_ms(feed_time);
//
motor = 0;
}
}
}
debounce = 0;
jack = 0;
motor = 0;
}
else
{
// wait for Ok..
while (start_key_1 == 0 || start_key_2 == 0 || tctrl_evout == 1 || mgnt_sensor == 0);
error = led_error = 0;
// timer setting ~ 50 ms
TCNT1H = 0x0B;
TCNT1L = 0xDC;
}
}
}
/*--------------------------------------------------*/
if (tctrl_evout == 0)
{
error = led_error = 0;
}
else
{
error = led_error = 1;
}
press_time = (signed int)((read_adc(0) * 9.766) - 20.001); // press time: 0~10,000ms
feed_time = (signed int)((read_adc(1) * 0.049) - 00.000); // feed time: 0~50ms
if (press_time < 0)
{
press_time = 0;
}
if (feed_time < 0)
{
feed_time = 0;
}
lcd_clear();
sprintf(buf, "PT:%d FT:%d", press_time, feed_time);
lcd_puts(buf);
debounce = 0;
// timer seting ~ 50 ms
TCNT1H = 0x0B;
TCNT1L = 0xDC;
}
}
/****************************************************/