Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Resetting counter variable in MikroBasic PIC12F683

Status
Not open for further replies.

walumbe

Junior Member level 2
Junior Member level 2
Joined
Oct 15, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
174
Hi,

I am using MikroBasic Pro to program a PIC12F683 to turn on an output(GP2) for a given time (T) when two inputs(GP0 and GP1) both high. I would like to reset the time (T) when there is a positive going trigger at GP1 that is tied to a vibration switch and GP0 is high. Is there a function that handles this? I have tried using a counter variable with a time delay but that does not seem to work. The output LED turns off aftere time (T). Below is the code that I have so far.


Code:
dim count as byte  'define variable count as byte

sub procedure Led_On
    if Button(GPIO, 1, 10, 1) then 'detect HIGH on GP1
          GPIO.B2 = 1
          Delay_ms(10000)
          else
    end if
end sub

sub procedure Check_Vibration     'If GP1 is high reset counter
     if Button(GPIO, 1, 10, 1) then
          count = 0
          else
    end if
end sub
      
main:
  ANSEL = 0
  ADCON0 = 0
  CMCON0 = 7

  TRISIO = %00000011   'Set GP0 and GP1 as inputs , GP2 as output

  GPIO.B0 = 0           'Initial GP0 value -> Input Dark Sensor
  GPIO.B1 = 0           'Initial GP1 value -> Input Vibration Sensor
  GPIO.B2 = 0           'Initial GP2 value -> Output LED
  
  count = 0               'Initialize count
  TestPin = 0             'Initialize value of Pin6

  while   True         
        do
         if Button(GPIO, 0, 1, 1) then  'detect HIGH on GP0
            Led_On
            GPIO.B1 = 0
            Check_Vibration
            end if
         count = count + 1

       loop until count = 12
       GPIO.B2 = 0

  wend
end.
 

What is connected to GP0 and GP1? If you want to turn ON GP2 for a given time T when both GP0 and GP1 are high then you can use a 2-input and gate and connect the sensors connected to GP0 and GP1 to AND gate inputs and output of AND gate goes to external interrupt pin of micro-controller. You have to use both external interrupt and Timer interrupt to perform what you expect.
 

A dark sensor is connected to GP0 and a vibration sensor to GP1. When they are both high GP2 will go high and trigger the base of a transistor to turn on a LED board. Both inputs have to be active HIGH for the output to function. I have it working without the use of an external AND gate, but it does not reset the time delay when I want it to. I have used resistor networks to make the input values match the required threshold voltage for the PIC. I previously tried using the timer interrupt but got all sorts of inconsistent behavior with the circuit. This is my first attempt using the PIC and have some difficulty understanding the interrupts. Can I use interrupts to check the state of GP1 then pass that value to a variable that can be used to reset the counter in the code?


What is connected to GP0 and GP1? If you want to turn ON GP2 for a given time T when both GP0 and GP1 are high then you can use a 2-input and gate and connect the sensors connected to GP0 and GP1 to AND gate inputs and output of AND gate goes to external interrupt pin of micro-controller. You have to use both external interrupt and Timer interrupt to perform what you expect.
 

high and trigger the base of a transistor to turn on a LED board

If you just want to turn on an LED and your microcontroller output pin can source or sink 25 mA then you don't need the transistor. If not, is transistor is used to switch ON power to LED board? If yes, how many LEDs does the board have? Post a image of your circuit or the circuit in Proteus 7.x format.

Edit: I have done it in a different way using AND gate. mikroC project files + Proteus file attached.

Edit2: Oops, I didn't see that you are using Basic.


Code - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
program ledControl
 
' Declarations section
Dim int0Flag as Byte
    tmr1Flag as Byte
Dim counter as LongInt
 
sub procedure interrupt
 
   If INTCON.INTF THEN
           T1CON.TMR1ON = 1
           GPIO.B0 = 1
           INTCON.INTF = 0
   End If
 
   If TMR1IF_bit Then
        counter = counter + 1
        If counter = 8 Then
               tmr1Flag = 1
               counter = 0
               GPIO.B0 = 0
        End If
 
        TMR1H = 0x0B
        TMR1L = 0xDB
        TMR1IF_bit = 0
   End If
   
end sub
 
main:
'   Main program 
 
    TRISIO = %001100
    GPIO = $00
    ANSEL = $00
    CMCON0 = $07
    INTCON = $D0
    T1CON = $B8
    TMR1H = $0B
    TMR1L = $DB
    PIR1 = $00
    PIE1 = $01
    OPTION_REG.INTEDG = 1
 
    WHILE(TRUE)
          asm
               nop
          end asm
    WEND
    
end.

 

Attachments

  • PIC12F683 LED Control.rar
    22.8 KB · Views: 88
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top