NOOB. needs some help with code....

Post your requests and ideas on the future development of mikroBasic.
Post Reply
Author
Message
jamesk
Posts: 3
Joined: 18 Sep 2005 17:50

NOOB. needs some help with code....

#1 Post by jamesk » 18 Sep 2005 17:53

im trying to make a seven seg display increment everytime i press a switch down, it seems really easy but i just dont know where to start........Sorry this is my first time using this program...please help me!! :cry:

Sidharta
Posts: 233
Joined: 01 Sep 2005 10:53
Location: Spain

How to begin

#2 Post by Sidharta » 18 Sep 2005 20:10

The best way for me is to begin with the examples.
You need also a tarjet to test your code. The best way is to begin with a PIC with usart so you can test things with the terminal included in tools or with another terminal you are used to.
To pass data from a pic with usart to a PC you only need a MAX232 and a few circuits.
You can buy a card . There are a lot in mikroelectronica ,and they are not quite expensive or if you are good in soldering you can do yours. I think it's the best way to learn and to begin developping your own prototipes .
It's not difficult in fact you can use some of the hardware examples in the manual .
You can too test it theoretically with the debugger, but I don't recommend you to do it . A led blinking gives more satisfaction when you see it .
And when you can write "Hello world " from the pic to the PC is a real world of ectasy :)

jamesk
Posts: 3
Joined: 18 Sep 2005 17:50

#3 Post by jamesk » 19 Sep 2005 03:51

I already got a programer and got the seven segment display example and the LED example working. I just dont know the coded myself. I know i would take the seven seg display example and modify it but i dont really know how. I just need one seven seg display count up to nine with the push of a switch.please help me been trying to teak the examples but never seems to work.

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#4 Post by Charlie » 19 Sep 2005 10:23

Hi James,

What Port are you using?(PortB,C,D) and what Pic are you using as well?If you are using just one 7 segment display then it will be fairly easy to make it work.
Regards Charlie M.

jamesk
Posts: 3
Joined: 18 Sep 2005 17:50

#5 Post by jamesk » 19 Sep 2005 16:25

im using portA as an input portB as an output and im using the 16f84A PIC....... thanks alot for the help.

dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Driving seven seg LED

#6 Post by dangerous » 22 Sep 2005 16:32

Hi, Jamesk

First of all, construct a segment table. This equates a segment to a port pin.
Seg a is connected to Portb,0 b to portb,1 etc thru to g on Portb,6 (then dp on portb,7 if you like). I am assuming that you are using common cathode displays with the anodes to portb via about 100 ohm resistors and the cathode to 0V. Otherwise invert everything with a ULN2003 driver or similar and connect portb directly to the ULN input pins (it has internal resistors), cathodes to the outputs through 100R and ande to +5v.

"1" = a & b, "2" = a & g & c & d & f & g. etc These now correspond to hex numbers from 0 to ff. To activate segment a, turn portb,0 on, similar for seg b, turn on portb,1 etc.

You have to equate all segments to their appropriate port bit then control the port bits to activate the display, so if a = portb,0, b = portb,1, c = portb, 2, d = portb, 3 e = portb, 4 f = portb, 5 g = portb, 6 dp = portb,7.
these correpond to hex numbers as follows: a = 0x01, b= 0x02, c = 0x04, d = 0x08, e = 0x10, f= 0x20, g = 0x40, dp = 0x08.

To display segments c, f & g to show 3 bars add c (0x04) e 0x10) and g (0x40) which gives 0x45. so portb = 0x45 will show the three bars.
Using this reasoning you can make a table for each number.

These can be stored as (say Seg_tab) in an array.

const Seg_tab as byte[8] = (0x03,0x6D,0x67, etc to 0x80)

then in your program "1" = Seg_tab[0]
"2" = Seg_tab[1]
"3" - Seg_tab[2]
to
"dp"= Seg_tab[7]

Create a register: dim count as byte
increment counter on each push of porta button:
count = 0
redo:
portb = Seg_tab[count] 'first location , update display
pin:
if porta <> 0 then
inc (count)
Else goto pin
end if

goto redo

Something along these lines should work OK until count is greater than 9 when it will do odd things without correcting.

You have to think in port pins rather than numbers to drive the LEDs correctly.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#7 Post by xor » 23 Sep 2005 02:02

  • This code uses the DISPLAY1 Example in mBasic with some alterations. PORTA.0 is turned on, or high, to enable the first digit on the EasyPic board. PORTA.4 is set up as an input for pushbutton RA4. PORTB drives the 8 segments (the period or dot is the 8th segment).

Code: Select all

'******************************************************************************
' microcontroller  P16F877A
'
' Project display1
' This project is designed to work with PIC 16 MCU family
' with minor adjustments, it should work with any other PIC MCU.
'
'  This code demonstrates how to display number on one 7-segment display
'  (common cathode).
'  Display is connected to portb(RB0..RB7, segment A to RB0,
'  segment B to RB1, etc) common cathode is connected to the pin RA0 on porta.
'  Number is incremented pressing pushbutton on RA4
'******************************************************************************

dim  i as  byte

sub function mask(dim num as byte) as byte  ' this function returns mask
                                            ' of parameter 'num'
  select case num                           ' for common cathode 7-seg. display
    case 0  result = $3F
    case 1  result = $06
    case 2  result = $5B
    case 3  result = $4F
    case 4  result = $66
    case 5  result = $6D
    case 6  result = $7D
    case 7  result = $07
    case 8  result = $7F
    case 9  result = $6F
  end select'case end
end sub

main:

  INTCON = 0            ' Disable PEIE,INTE,RBIE,T0IE
  trisa  = $10          ' PortA is output except Pin4 is input
  trisb  = 0            ' PortB is output and is segment driver
  portb  = 0            ' PortB initialized at 0- Segments Off
  porta  = 1            ' Enables 1 7-Segment Display
  i = 0
  PORTB = $3F           'Start at 0
  
  While true            ' endless loop
    If PORTA.4 <> 0 Then     ' Pressing RA5 will increment value to be displayed
       Delay_ms(10)     ' Is the button really pressed? Check again after delay
       If PORTA.4 <> 0 Then
          i = inc(i)
          If i = 10 Then i = 0 End If  ' rollover to 0 if i = 10
          PORTB = mask(i)  ' PortB outputs correct segments to represent numeral
       End If
    End If
    Delay_ms(170)     ' delay helps prevent numbers running while pushing button
  Wend
  
end.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#8 Post by xor » 23 Sep 2005 02:31

  • This is the same program as above except that you can set up the 7-Segment PortB mask as a Byte Array of Constants. It produces a little smaller code.

Code: Select all

dim i as byte
const numeral as  byte[10] = ($3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F)

main:

  INTCON = 0            ' Disable PEIE,INTE,RBIE,T0IE
  trisa  = $10          ' PortA is output except Pin4 is input
  trisb  = 0            ' PortB is output and is segment driver
  porta  = 1            ' Enables 1 7-Segment Display
  i = 0
  portb = numeral[0]    'Start at 0
  
  While true            ' endless loop
    If PORTA.4 <> 0 Then     ' Pressing RA5 will increment value to be displayed
       Delay_ms(5)     ' Is the button really pressed? Check again after delay
       If PORTA.4 <> 0 Then
          i = inc(i)
          If i = 10 Then i = 0 End If  ' rollover to 0 if i = 10
          PORTB = numeral[i]  ' PortB outputs correct segments to represent numeral
       End If
    End If
    Delay_ms(180)     ' delay helps prevent numbers running while pushing button
  Wend
  
end.
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Post Reply

Return to “mikroBasic Wish List”