/********************************************* This program was produced by the CodeWizardAVR V1.0.2.1b Standard Automatic Program Generator © Copyright 1998-2001 Pavel Haiduc, HP InfoTech S.R.L. http://infotech.ir.ro e-mail:dhptechn@ir.ro , hpinfotech@xnet.ro Project : Test program for RT-AVR-board Version : 1.x Date : 26-9-2002 Author : E. Dertien Company : Twente University Comments: This is a simple program for testing the basic hardware functions of the RT-AVR-board. Assumed is that the jumpers for PORTA are in place so that the test switches and knob (potentiometer) are connected. A servo can be connected to PORTB.0 (first line, righthande side of the board) The servo has to be wired to the connector as follows: pen 1 GND (bottom-line), pen 2 VCC (middle), pen 3 signal (closest to the LED's). After switching on a text is displayed on the LCD, with the analogue value of the knob. This value sets the demanded servo-position. The two on board switches cause two LED's of PORTB to be turned on or off. When a piezo speaker is connected to PORTC.3 it will produce a short beep when one of the switches is pressed. Chip type : AT90S8535 Clock frequency : 8,000000 MHz Memory model : Small Internal SRAM size : 512 External SRAM size : 0 Data Stack size : 128 *********************************************/ #include <90s8535.h> // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 #endasm #include #include // Standard Input/Output functions #include #include // Global variables char servo; char servotemp; char freq; int duration; void beep(void); char *adcvalue; // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { // Reinitialize Timer's 0 value TCNT0=0x64; //reload timer at 50Hz servotemp = servo; if (servotemp == 0) servotemp = 1; PORTB.0 = 0; //make output HIGH (LED turns off) delay_us(800); //minimal pulse delay do { delay_us(5); //servo is regulated in 255 steps of 1ms/255 servotemp--; } while (servotemp > 0); PORTB.0 = 1; //make output LOW (LED turns on) } interrupt [TIM2_OVF] void timer2_ovf_isr(void) { TCNT2 = freq; //load timer PORTC.3 ^=1; //toggle PORTC.3 state duration --; if (duration <1) { //if duration is transceded, disable timer TIMSK &= 0xbf; //disable interrupt TCNT2=0; //clear timer PORTC.3=1; //turn output high (no permanent dc current through buzzer) } } #define ADC_VREF_TYPE 0x00 // Read the ADC conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input|ADC_VREF_TYPE; ADCSR|=0x40; while ((ADCSR&0x10)==0); ADCSR|=0x10; return ADCW; } void beep(void) { freq=0xdf; TCCR2 = 0x06; TCNT2 = freq; duration = 250; TIMSK |= 0x40; // enable intterrupt } void initservo(void) { TCCR0=0x05; TCNT0=0x64; //start timer 0 at 50Hz TIMSK=0x01; //enable timer interrupts } void main(void) { // Locals char adin6; char adin5; unsigned int temp; // Input/Output Ports initialization // Port A PORTA=0xFF; //enable all pull-up resistors DDRA=0x00; //define PORTA input // Port B PORTB=0x00; DDRB=0xFF; // Port C PORTC=0x00; DDRC=0x08; // Port D PORTD=0x00; DDRD=0xF2; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 7,813 kHz // Mode: Output Compare // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 1000,000 kHz // Mode: 8 bit Pulse Width Modulation // OC1A output: Non-Inv. // OC1B output: Non-Inv. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0xA1; TCCR1B=0x02; TCNT1H=0x00; TCNT1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Output Compare // OC2 output: Disconnected TCCR2=0x00; ASSR=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off GIMSK=0x00; MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // UART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // UART Receiver: On // UART Transmitter: On // UART Baud rate: 9600 UCR=0x18; UBRR=0x33; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; // ADC initialization // ADC Clock frequency: 500,000 kHz ADCSR=0x84; // LCD module initialization lcd_init(16); lcd_gotoxy(0,0); lcd_putsf("RT-AVRboard"); //display this string at the first line lcd_gotoxy(0,1); lcd_putsf("Position: "); // Global enable interrupts #asm("sei") initservo(); //initialise and start //timer for a servo on PORTB.0 while (1) //Program Loop starts here { lcd_gotoxy(12,1); //move to LCD position, second line, 12th pos temp = read_adc(7); //read adc-value of PORTA.7 temp = temp/4; //devide 10-bits adc-value to genereate char-value(0-255) adin6 = read_adc(6); //read two switches on PORTA.5 and PORTA.6 adin5 = read_adc(5); itoa(temp,adcvalue); //convert the value to a string lcd_puts(adcvalue); //print string-value on LCD-display putchar(temp); //print output-value to serial port lcd_putchar(' '); lcd_putchar(' '); if (adin6 == 0) { PORTB.7 = 1; //switch on a LED when key is pressed beep(); //generate short beep on PORTC.3 } else PORTB.7 = 0; if (adin5 == 0){ PORTB.6 = 1; //switch on a LED when key is pressed beep(); //generate short beep on PORTC.3 } else PORTB.6 = 0; servo = temp; //output the ADC-value to servo on PORTB.0 delay_ms(100); //loop delay }; }