'( ################################################################################ project: Simple FM Radio - RDA5807M chip -------------------------------------------------------------------------------- name : ContrlBrdTst.bas copyright : (c) Chris Hirt, OE3HBW, Austria, JN87AQ, 2022 purpose : Controller Modul Test (for FM Radio with RDA5807M) micro : ATmega644-20PU programmer : homebrew (STK200/STK300 emulation) compiler : MCS BASCOM-AVR 2.0.8.4 - 001 flash : 8% code build : V08 @ 28072022 (28. Jul. 2022) status : Test GLCD + Rotary Encoder + KeyPad + LEDs + Buzzer -------------------------------------------------------------------------------- ########################### HARDWARE ########################################### '----------------------------- uC ATmega644 PDIP40 20 MHz PIN 01 PB0 PORTB.0 T0 / XCK0 --> Rotary Encoder A 02 PB1 PORTB.1 T1 / CLKO --> Rotary Encoder B 03 PB2 PORTB.2 INT2 / AIN0 --> LED FM 04 PB3 PORTB.3 OC0A / AIN1 --> LED AM 05 PB4 PORTB.4 OC0B / SS --> LED Stereo 06 PB5 MOSI MOSI --> ISP 07 PB6 MISO MISO --> ISP 08 PB7 SCK SCK --> ISP 09 /RESET RST --> ISP 10 VCC +5V 11 GND 0V 12 XTAL2 Quartz 18.3420 MHz 13 XTAL1 Quartz 18.3420 MHz 14 PD0 PORTD.0 RXD --> Beeper HMB-06 Piezo-Buzzer 15 PD1 PORTD.1 TXD --> SW6 (RE switch) 16 PD2 PORTD.2 INT0 / RXD1 --> SINT Switch Interrupt 17 PD3 PORTD.3 INT1 / TXD1 --> SW1 18 PD4 PORTD.4 OC1B / XCK1 --> SW2 19 PD5 PORTD.5 OC1A --> SW3 20 PD6 PORTD.6 OC2B / ICP --> SW4 21 PD7 PORTD.7 OC2A --> SW5 22 PC0 PORTC.0 TWI SCL --> I2C Bus SCL RDA5807M 23 PC1 PORTC.1 TWI SDA --> I2C Bus SDA RDA5807M 24 PC2 PORTC.2 TCK --> GLCD RST 25 PC3 PORTC.3 TMS --> GLCD CS2 26 PC4 PORTC.4 TDO --> GLCD CS1 27 PC5 PORTC.5 TDI --> GLCD E 28 PC6 PORTC.6 TOSC1 --> GLCD RW 29 PC7 PORTC.7 TOSC2 --> GLCD DI 30 AVCC VCC +5V 31 AGDN GND 0V 32 AREF ADC REF 33 PA7 PORTA.7 ADC7 --> GLCD D7 34 PA6 PORTA.6 ADC6 --> GLCD D6 35 PA5 PORTA.5 ADC5 --> GLCD D5 36 PA4 PORTA.4 ADC4 --> GLCD D4 37 PA3 PORTA.3 ADC3 --> GLCD D3 38 PA2 PORTA.2 ADC2 --> GLCD D2 39 PA1 PORTA.1 ADC1 --> GLCD D1 40 PA0 PORTA.0 ADC0 --> GLCD D0 ----------------------------- GLCD TG12864B-03a 128x64 Blue/White PIN 01 VSS GND 02 VDD +5V 03 VO 04 DI 05 RW 06 E 07 D0 08 D1 09 D2 10 D3 11 D4 12 D5 13 D6 14 D7 15 CS2 16 CS1 17 RST 18 VEE 19 K LED GND 20 A LED +5V ----------------------------- Keypad + RE switch 5 x Key 1 x Rotary Encoder Switch 6 x Diodes for Interrupt 6 x 10n Internal Pull Up Resistor ----------------------------- Rotary Encoder "ddm427" A GND B 15 pulse / 360° CCW CW A B A B 1 1 1 1 1 0 0 1 0 0 0 0 0 1 1 0 1 1 1 1 ################################################################################ ') '$PROG &HFF,&HD7,&HD9,&HFC 'Take care - Fuse Bits !!! $regfile = "m644def.dat" 'ATmega644-20PU $crystal = 18342000 '18.4320 MHz 'stack and frame size not optimized! $hwstack = 200 $framesize = 400 $swstack = 300 $lib "glcdKS108.lbx" 'include GLCD Lib $lib "I2C_TWI.lbx" 'include TWI Lib --> force BASCOM to use the hardware TWI '----- Config I2C -------------------------------------------------------------- Config SDA = PortC.1 'Configures a port pin for use as serial data SDA Config SCL = PortC.0 'Configures a port pin for use as serial clock SCL Config Twi = 400000 'TWI = I2C clock speed is 400 kHz '----- Config GLCD ------------------------------------------------------------- Config GraphLCD = 128 * 64sed , Dataport = PortA , Controlport = PortC , _ Cd = 7 , Rd = 6 , Ce = 3 , Ce2 = 4 , Enable = 5 , Reset = 2 '----- Config Keypad ----------------------------------------------------------- DDRD = &b0000_0000 PORTD = &b1111_1010 'SW1-6 PullUp enabled Config Int0 = Falling On Int0 Isr_keypad 'Interrupt-Routine Externer Int0 --> PD2 '----- Config Rotary Encoder --------------------------------------------------- DDRB.0 = 0 'PB.0 as Input PortB.0 = 1 'and PullUp enabled for Rotary Encoder A DDRB.1 = 0 'PB.1 as Input PortB.1 = 1 'and PullUp enabled for Rotary Encoder B Config Timer0 = Timer , Prescale = 256 On Timer0 Isr_Timer0 'Timer0 Interrupt service routine on overflow '----- Config LEDs and Sound --------------------------------------------------- LEDFM Alias Portb.2 Config LEDFM = Output 'Orange LED LEDAM Alias Portb.3 Config LEDAM = Output 'Green LED LEDST Alias Portb.4 Config LEDST = Output 'Red LED Beeper Alias PortD.0 Config Beeper = Output 'Buzzer HMB-06 2.2kHz 5V 20mA '------------------------------------------------------------------------------- Dim a As Byte Dim sw As Byte Dim KeyCode As Byte Dim REstate As Byte Dim REold As Byte Dim REnew As Byte Dim CStep As Integer Dim Cnt As Long Dim lc As Byte Declare Sub InitDisplay() Declare Sub ShowBox() Declare Sub DrawIntro() Declare Sub DisplaySW() Declare Sub DisplayRE() Declare Sub Beep() '####################### Main ################################################## REold = 0 'Rotary Encoder status REnew = 0 REstate = 0 a = 0 'Action code for nothing Keycode = &hFA 'KeyCode for nothing = b1111_1010 sw = 0 lc = 1 'first loop flag Beeper = 0 LEDAM = 0 'green LED OFF - symbolizes AM OFF LEDFM = 0 'orange LED OFF - symbolizes FM OFF LEDST = 0 'red LED OFF - symbolizes Stereo OFF Cnt = 1000 'start with 1000 only for test CStep = 10 'CStep = 10 only for test Enable Int0 Enable Timer0 Enable Interrupts Call InitDisplay() 'initializing Graphic LED Display Call DrawIntro() 'draw intro display '----- Main Loop ----- Do Do 'monitor user action If REstate <> 0 Then a = 1 'wait until RE is moved If KeyCode <> &hFA Then a = 1 'wait until key pressed Loop Until a <> 0 If lc = 1 Then 'first loop is true Cls Call ShowBox() 'show static display parts End If Disable timer0 'stop Timer0 for display RE state If REstate = 2 Then Cnt = Cnt + CStep 'Encoder increment Call DisplayRE() 'show the state of RE End If If REstate = 1 Then Cnt = Cnt - CStep 'Encoder decrement Call DisplayRE() 'show the state of RE End If Enable Timer0 'enable Timer0 Select Case KeyCode 'Keypad with hardware INT (Int0) Case &hF2: 'SW1 b1111_0010 sw = 1 Call Beep() LEDAM = 1 'green LED ON - symbolizes AM ON LEDFM = 0 'orange LED OFF - symbolizes FM OFF LEDST = 0 'red LED OFF - symbolizes Stereo OFF Call DisplaySW() 'show key 1 state Case &hEA: 'SW2 b1110_1010 sw = 2 Call Beep() LEDAM = 0 LEDFM = 1 LEDST = 0 Call DisplaySW() 'show key 2 state Case &hDA: 'SW3 b1101_1010 sw = 3 Call Beep() LEDAM = 0 LEDFM = 0 LEDST = 1 Call DisplaySW() 'show key 3 state Case &hBA: 'SW4 b1011_1010 sw = 4 Call Beep() LEDAM = 1 LEDFM = 1 LEDST = 0 Call DisplaySW() 'show key 4 state Case &h7A: 'SW5 b0111_1010 sw = 5 Call Beep() LEDAM = 0 LEDFM = 1 LEDST = 1 Call DisplaySW() 'show key 5 state Case &hF8: 'SW6 b1111_1000 sw = 6 Call Beep() LEDAM = 1 LEDFM = 1 LEDST = 1 Call DisplaySW() 'show key 6 state End Select KeyCode = &hFA 'reset KeyCode a = 0 'reset action flag REstate = 0 'reset incremental flag lc = 2 'out of first loop Loop End 'of program '######################## Subs ################################################# Sub Beep() 'short beep with 2.2kHz Beeper = 1 Waitms 150 Beeper = 0 End Sub Sub InitDisplay() 'init graphic display Glcdcmd &h3E , 1 : Glcdcmd &h3E , 2 'display off Waitms 100 'wait 50ms Glcdcmd &h3F , 1 : Glcdcmd &h3F , 2 'display on Setfont Font8x8 'load font 8x8 Cls 'clear display End Sub Sub ShowBox() 'show static display parts Box(0 , 0) -(127 , 63) , 1 'white border Boxfill(0 , 0) -(127 , 20) , 1 'white rectangle End Sub Sub DrawIntro() 'draw intro picture Showpic 0 , 0 , Intropic 'load a BGF coded intro picture Wait 3 'wait 3 sec Cls Call ShowBox() 'show static display parts Lcdat 2 , 2 , "Controller Test" , 1 'show intro text Lcdat 4 , 2 , " ATmega644 " , 0 Lcdat 6 , 2 , " OE3HBW 2022 " , 0 End Sub Sub DisplaySW() 'show key state Lcdat 2 , 2 , " Switch Test " , 1 Lcdat 4 , 2 , " " , 0 Lcdat 5 , 5 , "Key No: " , 0 Lcdat 5 , 55 , sw , 0 Lcdat 6 , 2 , " " , 0 End Sub Sub DisplayRE() 'show RE state Lcdat 2 , 2 , " RE Test " , 1 'Text Lcdat 5 , 5 , " " , 0 If REstate = 1 Then Lcdat 4 , 5 , "Left CCW" , 0 If REstate = 2 Then Lcdat 4 , 5 , "Right CW" , 0 Lcdat 6 , 5 , "RE pulse" , 0 Lcdat 6 , 70 , Cnt , 0 End Sub '---------------------- Interrupt service requests ----------------------------- Isr_keypad: 'ISR - Keypad hardware INT KeyCode = PinD AND &b1111_1010 'read PortD and mask Return Isr_Timer0: 'ISR - Rotary Encoder Timer0 = 0 'Timer preset REnew.0 = PINB.0 'read RE A hardware REnew.1 = PINB.1 'read RE B hardware If REnew <> REold Then 'Rotary Encoder is turned 'Detection of the direction of rotation If REnew = &b0000_0000 AND REold = &b0000_0001 Then REstate = 2 'CW If REnew = &b0000_0000 AND REold = &b0000_0010 Then REstate = 1 'CCW If REnew = &b0000_0011 AND REold = &b0000_0010 Then REstate = 2 'CW If REnew = &b0000_0011 AND REold = &b0000_0001 Then REstate = 1 'CCW REold = REnew 'Update status End If Return '----- GLCD Data --------------------------------------------------------------- Intropic: $bgf "Tuner.bgf" $include "font8x8.font" '############################ End Of File ######################################