'-------------------------------------------------------------------------------- 'name : M168_KGLCD_M1284_TimeGen_Control1.bas 'correspond to : M1284_KGLCD_TimeGen_tst3.bas 'purpose : Command control test1 'micro controller : Mega168 'board : ATMega168 'communication : TTL level RS232C (by USART, TXD/RXD) ' ' target control GLCD (GLCD is controlled by the other board: M1284) ' Kyocera KCG047QV1AE: 320 x 240 dots RGB 1,1,1 bit ' 8x8 character pattern corresponds to 40 char. x 30 line. (40 column x 30 row) ' Color bits, i.e. R/G/B total 3-bits, are embedded in 8-bit. '-------------------------------------------------------------------------------- ' LCD control constants ' 320x3/8 = 120B -> 128 units for line address to acces SRAM (7-address-bits). ' 240 lines -> 256 units for register set to access SRAM upper address (8-address-bits). '-------------------------------------------------------------------------------- Rem Used ports ' ' TWI I/F (un-used) ' ' PC5 : SCL ' PC4 : SDA ' ' ' User I/F ' ' PD2 : push SW1 (L=ON) ' PD3 : push SW2 (L=ON) ' PB1 : LED ' ' LCD (used for test information display) ' ' PC0-3 : LCD - (SC1602 character LCD) ' PD7 : LCD E ' PD6 : LCD RS ' NOTE : SC1602 LCD pin-5 was connected to GND. ' ' RXD & TXD = PD0 & PD1: cross connect with GLCD control Mega1284 RXD0, TXD0. '-------------------------------------------------------------------------------- ' ' Command to external AVR (M1284 GLCD control board) ' (Commands are ASCII strings via serial line, i.e., TXD0.) ' ' Cn is command. And following data are parameters. (n = number (ASCII)) ' Command format is ' Cn space Parameter1 , parameter2, ... ' Regarding to Command, there is no parameter. ' ' Command format examples: ' ' C0 [CR/LF] -> LED brink on GLCD control board. ' C1 [CR/LF] -> clear GLCD screen ' C2 dot_color [CR/LF] -> set dot color. ' C3 X1, Y1 [CR/LF] -> set dot coordinate (X1, Y1). ' ' Command sequence ' ' Main AVR (Mega168: this board) Slave AVR (External Mega1284) ' ' Send Command Get Command ' Input serial line (Wait) Execute the command ' Return "ACK" string ' Get "ACK" (via RXD) Wait the next command (Input serial line) ' ' NOTE: If command execution error, "ERR" not "ACK" is returned. ' ' ' The followings are command strings to control the Slave AVR. ' 'C0: Communication OPEN test. Only return "ACK" from slave. ' This command is used to test open communication, ' since the PRINT command in slave AVR outputs NULL string ' just after Power-ON or Reset of the slave AVR. ' Once after the output of NULL string, PRINT command become normal. ' Therefore, Main AVR should try multiple C0 commands until normal "ACK" is returned. 'C1: LED blink (Communication test2) 'C2: clear screen 'C3 dot_color: set dot color 'C4 X1, Y1: Set (X1, Y1) coordinate data 'C5 X2, Y2: Set (X2, Y2) coordinate data 'C6: set a pixel at (X1, Y1) with dot_color 'C7: draw a line from (X1, Y1) to (X2, Y2) 'C8 radius: set Radius of circle 'C9: draw a circle. Center = (X1, Y1), Radius = size. ' 'C10 char_color, back_color: set character color & back color 'C11 Column, Row: Set character column & row 'C12 string: Print the string to (column, row). And column++. 'C13: (future extentions) 'C14: 'C15: 'C16: 'C17: 'C18: 'C19: ' 'NOTE: For undefined command numbers (#12~19), only "ACK" is returned. '-------------------------------------------------------------------------------- ' '-------------------------------------------------------------------------------- ' ' display modes (increment by SW1, start by SW2) ' ' Mode 0 : Open communication test ' 1 : communication test2 (Toggle LED on the Slave board) ' 2 : clear GLCD screen ' 3 : draw dots ' 4 : draw line ' 5 : print color character strings ' 6 : draw circle '-------------------------------------------------------------------------------- $regfile = "m168def.dat" $crystal = 8000000 ' 8MHz Internal OSC $hwstack = 64 $swstack = 64 $framesize = 60 $baud = 9600 Rem Constants ' color data definitions for each 1-bit RGB pixel. ' Data order is R = , G = , b = . Const White = 7 Const Black = 0 Const Red = 4 Const Green = 2 Const Blue = 1 Const Yellow = 6 Const Sky = 3 Const Violet = 5 Const Dly2 = 200 ' SW read wait time [ms] Rem USART0 & Communication defs. Config Input = Crlf , Echo = Crlf Echo Off Rem I/O def. Config Portd.2 = Input ' /SW1 (/INT0) Config Portd.3 = Input ' /SW2 (/INT1) Sw1 Alias Pind.2 Sw2 Alias Pind.3 Portd = &B0000_1100 ' pull-up Config Portb.1 = Output Led Alias Portb.1 Rem Liquid Crystal Display (LCD) pin number definition Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 Config Lcdpin = Pin , E = Portd.7 , Rs = Portd.6 Config Lcd = 16 * 2 ' for working Dim I As Integer , J As Integer Dim B As Byte Dim S As String * 64 Dim S1 As String * 64 , S2 As String * 64 Dim Mode_n As Byte Dim Tmcount As Integer ' timer counts Rem define Timer interrupt to sense Serial-Communication OverTime Config Timer1 = Counter , Prescale = 256 , Clear Timer = 1 , Compare A = Disconnect ' (prescale = 256 at 8MHz CK) means 31.25kHz timer CK. ' Therefore, 31250 counts corresponds to 1 sec. Stop Timer1 Timer1 = 0 Compare1a = 31249 ' 31250-1 On Compare1a Tm1_int ' Interrupt subroutine definition Disable Compare1a Rem program start Rem I/O initialize Reset Led Cls Lcd "Control test1" Rem test Reset Led For I = 1 To 4 Toggle Led Waitms 300 Next I Rem clear serial buffer Do B = Inkey() Loop Until B = 0 Print ' send Null string Rem initialize variables Mode_n = 0 Rem start Gosub Mdisp ' mode display Main: If Sw1 = 0 Then ' SW1 pushed Incr Mode_n If Mode_n > 6 Then Mode_n = 0 Gosub Mdisp Waitms Dly2 Bitwait Sw1 , Set ' SW1 is released Waitms Dly2 End If If Sw2 = 0 Then ' SW2 pushed Gosub Command_out Waitms Dly2 Bitwait Sw2 , Set Waitms Dly2 End If Goto Main Rem subroutines ' mode display ' Mode 0 : Open communication test ' 1 : communication test2 (Toggle LED on the Slave board) ' 2 : clear GLCD screen ' 3 : draw dots ' 4 : draw line ' 5 : print color character strings ' 6 : draw circle Mdisp: Cls Select Case Mode_n Case 0: Lcd "Open COM test" Case 1: Lcd "COM test2" Case 2: Lcd "Clear GLCD" Case 3: Lcd "Draw Dots" Case 4: Lcd "Draw line" Case 5: Lcd "Print Strings" Case 6: Lcd "Draw a Circle" End Select Return Rem Start by SW2 ' Command out ' Mode 0 : Open communication test ' 1 : communication test2 (Toggle LED on the Slave board) ' 2 : clear GLCD screen ' 3 : draw dots ' 4 : draw line ' 5 : print color character strings ' 6 : draw circle Command_out: Select Case Mode_n Case 0: ' Open Com. test Tmcount = 0 Timer1 = 0 Enable Compare1a Enable Interrupts ' 1-sec. timer enable Start Timer1 ' clear garbage in input buffer Do B = Inkey() Loop Until B = 0 Cmout02: Print "C0" Input S If S <> "ACK" Then Goto Cmout02 Disable Interrupts Disable Compare1a Stop Timer1 Lowerline Lcd S Case 1: ' Com. test2 (LED toggle on the target board) Print "C1" Input S Lowerline Lcd S Case 2: ' clear GLCD Print "C2" Input S Lowerline Lcd S Case 3: ' draw dots Lowerline Print "C3 7" ' dot color=white Input S If S <> "ACK" Then Goto Cerror ' error Lcd S Print "C4 100,100" ' (X,Y) Input S Lcd S If S <> "ACK" Then Goto Cerror Print "C6" ' draw a pixel Input S If S <> "ACK" Then Goto Cerror Lcd S Case 4: ' draw a line Print "C3 2" ' dot color=green Input S If S <> "ACK" Then Goto Cerror ' error Print "C4 100,100" ' (X1,Y1) Input S If S <> "ACK" Then Goto Cerror Print "C5 200,200" ' (X2,Y2) Input S If S <> "ACK" Then Goto Cerror Print "C7" ' draw a line Input S If S <> "ACK" Then Goto Cerror Case 5: ' color characters Print "C10 4,0" ' red char. Input S If S <> "ACK" Then Goto Cerror ' error Print "C11 1,2" ' Col, Row Input S If S <> "ACK" Then Goto Cerror Print "C12 Color graphic LCD from Kyoritsu Digit" ' String print Input S If S <> "ACK" Then Goto Cerror Print "C10 6,1" ' yellow char. & blue background Input S If S <> "ACK" Then Goto Cerror Print "C11 1,10" ' Col, Row Input S If S <> "ACK" Then Goto Cerror Print "C12 Kyocera KCG047QV1AE color LCD module." ' String print Input S If S <> "ACK" Then Goto Cerror Print "C10 7,0" ' white char. Input S If S <> "ACK" Then Goto Cerror Print "C11 5,20" ' Col, Row Input S If S <> "ACK" Then Goto Cerror Print "C12 Command control via serial I/F" ' String print Input S If S <> "ACK" Then Goto Cerror Case 6: ' draw a circle B = Sky S2 = Str(b) ' string from numeric value S1 = "c3 " ' lower-case character 'c' is OK. S1 = S1 + S2 Print S1 ' dot color=sky Input S If S <> "ACK" Then Goto Cerror ' error Print "C4 150, 150" ' (X1,Y1) Input S If S <> "ACK" Then Goto Cerror Print "C8 80" ' (X2,Y2) Input S If S <> "ACK" Then Goto Cerror Print "C9" ' draw a circle Input S If S <> "ACK" Then Goto Cerror End Select Return Cerror: ' command error Lowerline Lcd S Return End Rem interrupt subroutines ' 1-sec. Timer interrupt Tm1_int: Incr Tmcount If Tmcount > 10 Then ' cannot communicate with Slave during 10 sec. Lowerline Lcd "Timeout ERROR" Stop End If Return Rem data end