Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Conto alla rovescia con lcd e 4x4 keypad

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] Conto alla rovescia con lcd e 4x4 keypad

Messaggioda Foto UtenteMtt0836 » 27 nov 2012, 22:56

Salve a tutti,
io gioco a Soft-air tutte le domeniche, vito che a turno dobbiamo inventarci delle giocate sto provando a preparare un mil-sim(simulazione) di un'attacco terroristico con una FINTA bomba, la quale deve essere accesa e programmato il codice dalla prima squadra e successivamente trovata e disattivata tramite codice o scollegare la batteria dalla seconda squadra.
io ho studiato elettronica a scuola e ci hanno insegnato molto leggermente la programmazione dei PIC.
Passo al dunque, vi chiedo se potete aiutarmi per cortesia a realizzare questo progetto, io ho a disposizione questi strumenti:
Schede con PIC
http://www.futurlec.com/PIC18F8722_Cont ... ical.shtml
oppure
http://www.futurlec.com/PIC16F877_Controller.shtml
entrambe le schede hanno quarzi da 10MHz
PIC:
18f2550
16f84A
12f683
lcd 2x 16:
http://www.futurashop.it/allegato/1446- ... Vt=&d=8,70
4x4 keypad
il resto ho già in casa resistenze condensatori transistor etcc..
per ora tramite mikrobasic ho scritto il seguente codice:
Codice: Seleziona tutto
program keypad_e_lcd

dim kp, cnt, oldstate as byte
    txt as char[7]

' Keypad module connections
dim keypadPort as byte at PORTC
' End Keypad module connections

' Lcd module connections
dim LCD_RS as sbit  at RB4_bit
    LCD_EN as sbit  at RB5_bit
    LCD_D4 as sbit  at RB0_bit
    LCD_D5 as sbit  at RB1_bit
    LCD_D6 as sbit  at RB2_bit
    LCD_D7 as sbit  at RB3_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit
' End Lcd module connections

main:
  oldstate = 0
  cnt = 0                             ' Reset counter
  Keypad_Init()                       ' Initialize Keypad

  Lcd_Init()                          ' Initialize LCD
  Lcd_Cmd(_LCD_CLEAR)                 ' Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF)            ' Cursor off
  Lcd_Out(1,1, "Padovankorps")
  Lcd_Out(2,1, "Soft-Air Team")
  delay_ms (1500)
  Lcd_Out(1,1, "Simbomb V2")
  Lcd_Out(2,1, "Made by Stord")
  delay_ms (1500)
  Lcd_Out(1, 1, "Key  :")             ' Write message text on LCD
  Lcd_Out(2, 1, "Times:")

  while TRUE

    kp = 0                            ' Reset key code variable

    ' Wait for key to be pressed and released
    while ( kp = 0 )
      kp = Keypad_Key_Click()         ' Store key code in kp variable
    wend
    ' Prepare value for output, transform key to it"s ASCII value
    select case kp
      'case 10: kp = 42   ' "*"       ' Uncomment this block for keypad4x3
      'case 11: kp = 48   ' "0"
      'case 12: kp = 35   ' "#"
      'default: kp += 48

    case 1
      kp = 49  ' 1                    ' Uncomment this block for keypad4x4
    case 2
      kp = 50  ' 2
    case 3
      kp = 51  ' 3
    case 4
      kp = 65  ' A
    case 5
      kp = 52  ' 4
    case 6
      kp = 53  ' 5
    case 7
      kp = 54  ' 6
    case 8
      kp = 66  ' B
    case 9
      kp = 55  ' 7
    case 10
      kp = 56  ' 8
    case 11
      kp = 57  ' 9
    case 12
      kp = 67  ' C
    case 13
      kp = 42  ' *
    case 14
      kp = 48  ' 0
    case 15
      kp = 35  ' #
    case 16
      kp = 68  ' D

    end select

    if (kp <> oldstate) then        ' Pressed key differs from previous
      cnt = 1
      oldstate = kp
    else                            ' Pressed key is same as previous
      Inc(cnt)
    end if
    Lcd_Chr(1, 10, kp)              ' Print key ASCII value on LCD
   

    if (cnt = 255) then             ' If counter varialble overflow
      cnt = 0
      Lcd_Out(2, 10, "     ")
    end if

    WordToStr(cnt, txt)             ' Transform counter value to string
    Lcd_Out(2, 10, txt)             ' Display counter value on LCD
  wend

end.

Il tutto usando le librerie e logica... quello che ora mi manca è settare il registro e il prescaler e creare il countdown e il controllo della chiave...
Vi ringrazio per qualsiasi consiglio/critica/aiuto che mi potete dare...
Avatar utente
Foto UtenteMtt0836
0 2
 
Messaggi: 6
Iscritto il: 8 mar 2012, 13:48

0
voti

[2] Re: Conto alla rovescia con lcd e 4x4 keypad

Messaggioda Foto UtenteLesStrato » 28 nov 2012, 15:47

Ciao conosco poco i PIC , ho sempre utilizzato gli avr , concettualmente le funzionalità sono le stesse ma cambiano i codici...se utilizzi mikrobasic dovresti aver vita facile direi , di certo non lavorerai a basso livello...come lcd immagino utilizzi uno standard hitachi hd44780 vero? Occhio ai cloni ;-) spesso girano lcd nn del tutto compatibili , i comandi sono sostanzialmente gli stessi ma differiscono ad esempio per inizializzazione e tempistiche, ti consiglio (per esperienza personale) di montare un quarzo esterno per maggiore accortezza...comunque ti consiglio di recuperare sia il datasheet dell'hd44780 (è molto chiaro e ti spiega passo passo come inviare dati e comandi al display) che del tuo mcu , soltanto così troverai le informazioni su come strutturare il tuo codice...A quanto ho capito ti serve un countdown , beh la maggior parte dei mcu possiede dei timer da settare , oppure puoi impostare un contatore a decremento...ciao ;-)
Avatar utente
Foto UtenteLesStrato
-13 3
 
Messaggi: 35
Iscritto il: 20 mar 2012, 23:48

0
voti

[3] Re: Conto alla rovescia con lcd e 4x4 keypad

Messaggioda Foto UtenteMtt0836 » 28 nov 2012, 17:52

io non ho mai visto gli avr ma se tu sei in grado di aiutarmi con il codice me ne compro uno e uso quello come MCU
Avatar utente
Foto UtenteMtt0836
0 2
 
Messaggi: 6
Iscritto il: 8 mar 2012, 13:48

0
voti

[4] Re: Conto alla rovescia con lcd e 4x4 keypad

Messaggioda Foto UtenteMtt0836 » 28 nov 2012, 23:10

ho continuato a programmare e ho scritto molto sta volta... (ispirazione)
potete controllare se secondo voi è giusto?
Codice: Seleziona tutto
program keypad_e_lcd

dim kp, cnt, oldstate, stato_ora as byte
    txt as char[7]
    password as string[7]
    hh   as integer
    mm   as integer
    ss   as integer
    ss1  as string[2]
    mm1  as string[2]
    hh1  as string[2]
    tempo as string[8]
    i as integer
   
   
' Keypad module connections
dim keypadPort as byte at PORTD
' End Keypad module connections

' Lcd module connections
dim LCD_RS as sbit  at RB4_bit
    LCD_EN as sbit  at RB5_bit
    LCD_D4 as sbit  at RB0_bit
    LCD_D5 as sbit  at RB1_bit
    LCD_D6 as sbit  at RB2_bit
    LCD_D7 as sbit  at RB6_bit

    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB6_bit
' End Lcd module connections
  Sound_Init(PORTA, 1)  ' Initialize sound at RA1
main:
   stato_ora = 1
  oldstate = 0
  cnt = 0                             ' Reset counter
  Keypad_Init()                       ' Initialize Keypad

  Lcd_Init()                          ' Initialize LCD
  Lcd_Cmd(_LCD_CLEAR)                 ' Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF)            ' Cursor off
  Lcd_Out(1,1, "Padovankorps")
  Lcd_Out(2,1, "Soft-Air Team")
  delay_ms (1500)
  Lcd_Out(1,1, "Simbomb V2")
  Lcd_Out(2,1, "Made by Stord")
  delay_ms (1500)
  Lcd_Out(1, 1, "Inserire Pass")             ' Write message text on LCD
  Lcd_Out(2, 1, "           ")
  Sound_Play(2500, 1000)
   delay_ms (100)
  while TRUE

    kp = 0                            ' Reset key code variable

    ' Wait for key to be pressed and released
    while ( kp = 0 )
      kp = Keypad_Key_Click()         ' Store key code in kp variable
    wend
    ' Prepare value for output, transform key to it"s ASCII value
    select case kp
      'case 10: kp = 42   ' "*"       ' Uncomment this block for keypad4x3
      'case 11: kp = 48   ' "0"
      'case 12: kp = 35   ' "#"
      'default: kp += 48

    case 1
      kp = 49  ' 1
      if (oldstate = 0) then
       password[cnt] =  "1"                  ' Uncomment this block for keypad4x4
      end if
     
        if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "1"
          end if
       
         if     (stato_ora =2)then
          hh1[2] = "1"
          oldstate = 2
          Stato_ora =1
         end if
        end if
         
                if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "1"
          end if

         if     (stato_ora =2)then
          mm1[2] = "1"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
  if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "1"
          end if

         if     (stato_ora =2)then
          ss1[2] = "1"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
        Sound_Play(2500, 100)
    case 2

      kp = 50  ' 2
      if (oldstate = 0) then
      password[cnt] = "2"
        end if
                  if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "2"
          end if

         if     (stato_ora =2)then
          hh1[2] = "2"
          oldstate = 2
          Stato_ora =1
         end if
        end if
         if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "2"
          end if

         if     (stato_ora =2)then
          mm1[2] = "2"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "2"
          end if

         if     (stato_ora =2)then
          ss1[2] = "2"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
        Sound_Play(2500, 100)
    case 3

      kp = 51  ' 3
      if (oldstate = 0) then
      password[cnt] = "3"
        end if
                if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "3"
          end if

         if     (stato_ora =2)then
          hh1[2] = "3"
          oldstate = 2
          Stato_ora =1
         end if
        end if
     if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "3"
          end if

         if     (stato_ora =2)then
          mm1[2] = "3"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "3"
          end if

         if     (stato_ora =2)then
          ss1[2] = "3"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
        Sound_Play(2500, 100)
    case 4
      kp = 65  ' A
      if (oldstate = 0) then
      password[cnt] = "A"


        end if
      Sound_Play(2500, 100)
    case 5
      kp = 52  ' 4
      if (oldstate = 0) then
      password[cnt] = "4"
      end if
          if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "4"
          end if

         if     (stato_ora =2)then
          hh1[2] = "4"
          oldstate = 2
          Stato_ora =1
         end if
        end if
     if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "4"
          end if

         if     (stato_ora =2)then
          mm1[2] = "4"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "4"
          end if

         if     (stato_ora =2)then
          ss1[2] = "4"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
       Sound_Play(2500, 100)
    case 6
      kp = 53  ' 5
      if (oldstate = 0) then
      password[cnt] = "5"

        end if
            if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "5"
          end if

         if     (stato_ora =2)then
          hh1[2] = "5"
          oldstate = 2
          Stato_ora =1
         end if
        end if
   if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "5"
          end if

         if     (stato_ora =2)then
          mm1[2] = "5"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "5"
          end if

         if     (stato_ora =2)then
          ss1[2] = "5"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
       Sound_Play(2500, 100)
    case 7
      kp = 54  ' 6
      if (oldstate = 0) then
     password[cnt] = "6"
             if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "6"
          end if

         if     (stato_ora =2)then
          hh1[2] = "6"
          oldstate = 2
          Stato_ora =1
         end if
        end if

       end if
      if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "6"
          end if

         if     (stato_ora =2)then
          mm1[2] = "6"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
         if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "6"
          end if

         if     (stato_ora =2)then
          ss1[2] = "6"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
       
      Sound_Play(2500, 100)
    case 8
      kp = 66  ' B
      if (oldstate = 0) then
      password[cnt] = "B"


        end if
       Sound_Play(2500, 100)
    case 9
      kp = 55  ' 7
      if (oldstate = 0) then
     password[cnt] = "7"

       end if
             if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "7"
          end if

         if     (stato_ora =2)then
          hh1[2] = "7"
          oldstate = 2
          Stato_ora =1
         end if
        end if
       
         if (oldstate = 2) then


         if     (stato_ora =2)then
          mm1[2] = "7"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then


         if     (stato_ora =2)then
          ss1[2] = "7"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
     Sound_Play(2500, 100)
    case 10
      kp = 56  ' 8
      if (oldstate = 0) then
      password[cnt] = "8"

        end if
              if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "8"
          end if

         if     (stato_ora =2)then
          hh1[2] = "8"
          oldstate = 2
          Stato_ora =1
         end if
        end if
       
        if (oldstate = 2) then


         if     (stato_ora =2)then
          mm1[2] = "8"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "8"
          end if

         if     (stato_ora =2)then
          ss1[2] = "8"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
       
        Sound_Play(2500, 100)
    case 11
      kp = 57  ' 9
      if (oldstate = 0) then
     password[cnt] = "9"

       end if
             if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "9"
          end if

         if     (stato_ora =2)then
          hh1[2] = "9"
          oldstate = 2
          Stato_ora =1
         end if
        end if
             if (oldstate = 2) then


         if     (stato_ora =2)then
          mm1[2] = "9"
          oldstate = 3
          Stato_ora =1
         end if
        end if
       
          if (oldstate = 3) then


         if     (stato_ora =2)then
          ss1[2] = "9"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
       
      Sound_Play(2500, 100  )
    case 12
      kp = 67  ' C
      if (oldstate = 0) then
     password[cnt] = "C"

     end if
      Sound_Play(2500, 100)
    case 13
      kp = 42  ' *

      Sound_Play(2500, 100)
    case 14
      kp = 48  ' 0
      if (oldstate = 0) then
      password[cnt] = "0"

        end if
              if (oldstate = 1) then
          if (stato_ora =1)then
            hh1[1] = "0"
          end if

         if     (stato_ora =2)then
          hh1[2] = "0"
          oldstate = 2
          Stato_ora =1
         end if
        end if
       
       if (oldstate = 2) then
          if (stato_ora =1)then
            mm1[1] = "0"
          end if

         if     (stato_ora =2)then
          mm1[2] = "0"
          oldstate = 3
          Stato_ora =1
         end if
        end if
          if (oldstate = 3) then
          if (stato_ora =1)then
            ss1[1] = "0"
          end if

         if     (stato_ora =2)then
          ss1[2] = "0"
          oldstate = 4
          Stato_ora =1
         end if
        end if
       
      Sound_Play(2500, 100)
    case 15
      kp = 35  ' #
      if (oldstate = 0) then
     password[cnt] = "#"

      end if
     Sound_Play(2500, 100)
    case 16
      kp = 68  ' D
      if (oldstate = 0) then
      password[cnt] = "D"

     
         end if
       Sound_Play(2500, 150)
    end select
    If (kp <> 0)and (kp <> 42) then
    cnt= cnt+1
  Lcd_Out(1,1, "Enter Password:")                       ' add a chr to display
  Lcd_Chr(2,cnt, kp)
     end if
    If  (kp = 42) then

  Lcd_Out(1,1, "Enter Password:")         'with * you delete the chr and return by 1
  Lcd_Out(2,cnt, " ")
  password[cnt]  = " "

  cnt = cnt-1

  end if
    if (kp= 68) then
   
     if (oldstate = 0) then
     oldstate = 1
      Lcd_Out(1, 1, "Inserire Tempo")             ' Write message text on LCD
      Lcd_Out(2, 1, "    HH:MM:SS    ")
 
      end if
      if (oldstate=4) then
      oldstate =5
      end if
    end if
      if (oldstate >1) then
      tempo[1]= hh1[1]
      tempo[2]=hh1[2]
      tempo[3]= ":"
      tempo[4]=mm1[1]
      tempo[5]=mm1[2]
      tempo[6]=":"
      tempo[7]=ss1[1]
      tempo[8]=ss1[2]
     
       Lcd_Out(2, 1,tempo)

      end if
      if (oldstate =5 ) then
          'rimane da fare il timer
      end if
  wend










end.
Avatar utente
Foto UtenteMtt0836
0 2
 
Messaggi: 6
Iscritto il: 8 mar 2012, 13:48


Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 5 ospiti