Well I'm back with a little less hair and on the verge of a cardiac arrest, no just kidding. The PIC code works well and I am happy with it but need to move everything over to Atmel.
Codevision has a utility to set up the three timers but I have no clue how to code in "c". How would I begin to set up the three timers and one adc channel on this Mega48 to
1. read the adc
2. pause 30 seconds or so (I might be able to figure this part out)
3. Read the ADC again and if it is lower than the first reading then raise the frequency of all three timers 10 Hz, and if it is higher than the first reading then lower the frequency 10 Hz.
That's the basics of the program but when it comes to all of the
>><<{{}} in "C" I obviously don't know beans about it. I have bought a C programming book that don't help me much! Anyhow after all the hours invested programming the 16F877 PIC I know what I am asking and I don't expect you all to drop everything and figure this out but if there is any code samples or ? that might point me in the right direction I would so much appreciate it. Thank you'all so much! Emmett
This is the basic pic code to do what I need (without the defines and stuff).
BEGIN:
For PWMFREQ = 150 To 300 Step 10
TIMER0,DUTY,PWMFREQ
ADCIN 0, CNTRL
PAUSE 60
ADCIN 0, TST
IF TST < CNTRL THEN PWMFREQ=PWMFREQ +10
PAUSE 60
ADCIN 0, TST
if TST >= CNTRL THEN PWMFREQ=PWMFREQ -10
NEXT PWMFREQ
goto BEGIN:
end
This is the setup generated by Codevision for the three timers to put out both non-inverted and inverted outputs
Project : three pwm's six outputs
Chip type : ATmega48
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
*****************************************************/
#include <mega48.h>
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
}
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
// Place your code here
}
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// 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
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=T
PORTB=0x00;
DDRB=0x0E;
// 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=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x08;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: Fast PWM top=FFh
// OC0A output: Non-Inverted PWM
// OC0B output: Inverted PWM
TCCR0A=0xB3;
TCCR0B=0x03;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
// Mode: Fast PWM top=01FFh
// OC1A output: Non-Inv.
// OC1B output: Inverted
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xB2;
TCCR1B=0x0C;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
// Mode: Fast PWM top=OCR0A
// OC2A output: Non-Inverted PWM
// OC2B output: Inverted PWM
ASSR=0x00;
TCCR2A=0xB3;
TCCR2B=0x0E;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x00;
EIMSK=0x00;
PCICR=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x01;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
// ADC4: On, ADC5: On
DIDR0=0x00;
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x82;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};