Code woes

marzan

Senior Member
Hello. I am trying to get a PCF8583 RTC to send data to an SAA1064.
Here is the code:

Code:
PAUSE 1000

  	i2cslave $A0, i2cslow, i2cbyte		'Initialize RTC
  	
  	writei2c 0,(%00000100)					
					
  	writei2c 8,(%00000010)					; alarm control registers and clock mode
									;bit7 	enable alarm interrupt
									;bit6		timer alarm
									;bit5/4	clock alarm 00= no clock alarm
									;bit3		timer interrupt enable
									;bit2/1/0  	timer function  001= hundredths of a second
						
					
	writei2c 2,(%00000000)					;reset minutes
	
	writei2c 1,(%00000000)					;reset milliseconds
	
	writei2c 3,(%00000000)					;reset milliseconds												
			
	DO
	
	i2cslave $A0, i2cslow, i2cbyte		'Initialize RTC
		
  	readi2c 0,(b1,b2,b3,b4,b5)
  	
  	debug
  	
  	i2cslave $70, i2cslow, i2cbyte
	
	b21 = b2//10
	
	b22 = b3/10
	
	b23 = b3//10
	
	LOOKUP b21, ($3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F),b7

	LOOKUP b23, ($BF,$86,$DB,$CF,$E6,$ED,$FD,$87,$FF,$EF),b8 ;DP

	LOOKUP b22, ($3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F),b9

	LOOKUP b4, ($BF,$86,$DB,$CF,$E6,$ED,$FD,$87,$FF,$EF),b11 ;DP
	
	writei2c $00, (%00010111,b11,b9,b8,b7)
	
	LOOP
It does the tenths ok (as far as I can tell) but the seconds go from 1 to 9 and then to 16. I guess it is an issue with displaying hex instead of binary. Can anyone point out where I have gone wrong ?

Marz.
 

SAborn

Senior Member
Have a look at using BCDTOASCII command to do the conversion before writing to the display.

If you also look under HELP in PE and you will find an ASCII Table that will help to show the difference between hex and Dec.
 

hippy

Technical Support
Staff member
Also start with two programs completely separate; getting the time reading is correct, and getting sending to the display right. For example; force values which are known rather than reading them from the RTC. There's nothing worse than debugging data that is always changing.
 

marzan

Senior Member
Also start with two programs completely separate; getting the time reading is correct, and getting sending to the display right. For example; force values which are known rather than reading them from the RTC.
I actually did this hippy. used a vaiable, incb1, used the same code with a pause50 to simulate something close to tenths of a second. That worked. then i tried reading the rtc using debug screen and that worked. previously I had an OLED reading the data from the RTC and had the same problem, but after about 4 hours of reading datasheets and trying different hings it started to work correctly. This was the code for the OLED:
Code:
symbol slvAddrWR = $A0  ' I2C write address

MAIN:

	PAUSE 1000

  	i2cslave slvAddrWR, i2cslow, i2cbyte		'Initialize RTC
  	
  	writei2c 0,(%00000100)					;timer function to 99 seconds
									;to enable %xx0xx1xx
  	writei2c 8,(%00000010)					; alarm control registers and clock mode
									;bit7 	enable alarm interrupt
									;bit6		timer alarm
									;bit5/4	clock alarm 00= no clock alarm
									;bit3		timer interrupt enable
									;bit2/1/0  	timer function  001= hundredths of a second
						
	serout b.7,n2400,(254,1)				;clear screen
	
	pause 30
	
	serout b.7,n2400,(254,148)				;move to line 3
	
	serout b.7,n2400,("TIME   ")	
					
	writei2c 2,(%00000000)					;reset minutes
	
	writei2c 1,(%00000000)					;reset milliseconds
	
	writei2c 3,(%00000000)					;reset milliseconds												
			
	DO
		
  	readi2c $0,(b1,b2,b3,b4,b5)
  	
  	BCDTOASCII b2,b10,b11
  	
	BCDTOASCII b3,b12,b13
	
	BCDTOASCII b4,b14,b15
  	
	serout b.7,n2400,(254,154)
	
	serout b.7,n2400,(b14,b15,":",b12,b13,":",b10,b11)

	LOOP
I can`t spot the difference other than the BCDtoascii. I read the datasheet for the RTC and it stores values as BCD. Is that not the same as binary? If I was to use bcdto ascii how would I change the lookup table? Or is i just better to covert it the way described in this thread:
http://www.picaxeforum.co.uk/showthread.php?13946-BCD-to-BINARY-Converter

Marz.
 

westaust55

Moderator
If you have an ASCII coded value (ie "0" to "9") and seek the binary value (0 to 9) then just subtract $30 for your pointer as I suggested in the thread you linked to.

If you try and use the ASCII value directly, you would need 47 entries in the LOOKUP table prior to the data for "0" (as "0" = $30 = 48 decimal) which would be wasteful of program space
 

marzan

Senior Member
Have a look at using BCDTOASCII command to do the conversion before writing to the display.
SAborn, As I have said in the other post I just put up, how do I then convert it to work with a lookup table if it has been converted to ascii, or is there another way that it can be done ? Maybe the code could be more efficient than it is. I`m still on a steep learning curve - but enjoying the journey. theres always something else to get your head around. I wish there was some kind of picaxe course in Adelaide.
 

marzan

Senior Member
So Westaust do you mean like this in another thread?

BCDTOASCII b0, b1, b2 ; BCD value in b0, tens into b1, units into b2
b1 = b1 - $30
b2 = b2 - $30

Marz.
 

hippy

Technical Support
Staff member
To get index values into the lookup table (0-9) from ASCII digits ("0"-"9") you could do the following ...

BCDTOASCII b2,b10,b11 : b10=b10-"0" : b11=b11-"0"
LOOKUP b10, ( ..... ), b10
LOOKUP b11, ( ..... ), b11
Output b10 and b11 to the 7-seg display

That first line works but is a bit inefficient, and you could instead do ...

b10 = b2 / 16
b11 = b2 & 15
LOOKUP b10, ( ..... ), b10
LOOKUP b11, ( ..... ), b11

The "b2 & 15" is a faster equivalent to "b2 // 16" so you can see how this is close to what you originally had but were using /10 and //10, base ten for decimal, rather than /16 and //16, base sixteen, which is what's required for BCD.
 

SAborn

Senior Member
I wish there was some kind of picaxe course in Adelaide.
Marzan,

I am not that far from you in location and dont mind a little phone 1 on 1 if it helps, also i have worked with RTC and the SAA1064, and also have working code from other projects for these that do what you are trying to do, but the code used is in long programs and not the most elegant which i dont want to post here.

If you dont find the answers or help you need via the forum then PM me and we will go from there.

BCD (or Binary Coded Decimal) is not the same as decimal, BCD is Hex.
 

marzan

Senior Member
Thanks SAborn, I might take you up on that in the future,as this is part of a bigger project that I want to build. I am doing it in stages so I can learn along the way without being overwhelmed by pages of code. so far I have this working on an OLED and now 7 seg. the big project is for wireless timing gear with a few add ons not seen in cheaper timing systems, but I digress...
Thanks everyone for steering me in the right direction. Hippy you gave me the final piece of the puzzle:
Code:
	PAUSE 500

  	i2cslave $A0, i2cslow, i2cbyte		'Initialize RTC
  	
  	writei2c 0,(%00000100)					
					
  	writei2c 8,(%00000010)					; alarm control registers and clock mode
									;bit7 	enable alarm interrupt
									;bit6		timer alarm
									;bit5/4	clock alarm 00= no clock alarm
									;bit3		timer interrupt enable
									;bit2/1/0  	timer function  001= hundredths of a second
						
					
	writei2c 2,(%00000000)					;reset minutes
	
	writei2c 1,(%00000000)					;reset milliseconds
	
	writei2c 3,(%00000000)					;reset milliseconds												
			
	DO
	
	i2cslave $A0, i2cslow, i2cbyte		'Initialize RTC
		
  	readi2c 1,(b2,b3,b4)
  	
  	debug
  	
  	i2cslave $70, i2cslow, i2cbyte
	
	b16 = b2 / 16
	
	LOOKUP b16, ($3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F),b16

	b18 = b3 / 16
	
	b17 = b3 & 15
		
	LOOKUP b17, ($BF,$86,$DB,$CF,$E6,$ED,$FD,$87,$FF,$EF),b17 ;DP

	LOOKUP b18, ($3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F),b18

	LOOKUP b4, ($BF,$86,$DB,$CF,$E6,$ED,$FD,$87,$FF,$EF),b19 ;DP
	
	writei2c $00, (%00010111,b19,b18,b17,b16)
	
	LOOP
Would it be worth putting this in the code snippets? (maybe with a few more comments)

Thanks again everyone for your replies.

Marz.
 

marzan

Senior Member
Just another quick question. Am I right in assuming the SAA1064 would be able to use these:

Brand New 7 Segment Red LED Display 3''
Common Anode

Size: 88.00 x 65.00 x 12.00 (mm)

Forward Voltage: 11V

Forward Current: 5-10mA

the datasheet - http://www.nxp.com/documents/data_sheet/SAA1064_CNV.pdf - on the second page says it is capable of handling 15v

is my assumption correct?
 

AllyCat

Senior Member
Hi,

Yes, it's probably suitable, but with the normal application diagram you may need a supply rail close to 15 volts (because of voltage drops across the drive transistors). If you want to use a 12 volt rail, there was a thread a few months ago (about using external PNP anode driver transistors) which I can probably find if necessary.

How many of the 7-segment displays are you planning to drive (from each chip)?

Cheers, Alan.
 

AllyCat

Senior Member
Hi Marz,

Yes, around posts #27 - #28 if you want to use a 12 volt rail.

Note that the SAA1064 only controls the current (i.e. brightness) in 3 mA steps (of cathode current), so you might not get much control range if the specified 5 - 10mA is intended as a peak current rating (but it probably isn't).

Cheers, Alan.
 

marzan

Senior Member
Thanks for the heads up Allan. I will be using the displays outside so I suspect they will have to be fairly bright. Maybe running the segments through a uln darlington array instead of connecting them directly to the controller might not be a bad idea considering the current?

Marz.
 

AllyCat

Senior Member
Hi Marz,

It's the usual question: "Do you have a data sheet for the displays?"

Yes, for a display of that size I would expect them to need quite a lot of current. The SAA max cathode drive current is 21mA which with the 2:1 multiplex averages about 10mA per segment. With 14 - 16 segments (per anode driver) that makes the possible anode (and supply) current over 300mA, before you consider "beefing up" the cathode drive.

Note that it's not necessarily the absolute "brightness" that you need to achieve but "contrast". Therefore it is sometimes worthwhile putting a tinted filter in front of the displays. For example, a 50% filter reduces the brightness by 50%, but most reflected light passes though the filter twice so the (unwanted) "background" illumination is reduced to 25%, doubling the contrast (and hopefully improving the readability).

Cheers, Alan.
 

SAborn

Senior Member
To follow on with Alan suggestion of a filter, i normally use red tinted acrylic 3mm thick, or for indoors use even red cellophane works well and can be layered to adjust the contrast.

If you make a front label for the display by printing it on paper with alloances to cut out a window for the display, then cellophane can be stuck to the rear of the paper, then laminate the paper sheet, this gives a sturdy front label with a nice red tinted window for the display, attach the label to the project box and it will look all very neat.

As for Acrylic there is several outlets at Lonsdale (somewhere near you) that sell off cuts of acrylic.
 

marzan

Senior Member
As for Acrylic there is several outlets at Lonsdale (somewhere near you) that sell off cuts of acrylic.
Yep. Only a couple of km from my place. I have seen coloured acrylic in their scrap bins before, but never thought to buy it. Thanks for the idea guys. I`ll have to give it a try when I get it all together. I think I even bought some acrylic cutting single flute bits for my mill a while ago.
 

SAborn

Senior Member
I think I even bought some acrylic cutting single flute bits for my mill a while ago.
I just use the circular saw with a fine tooth wood blade, it dont chip the edges too bad.

The display will look many times brighter with the red filter, i think its a must do for led displays.
 

AllyCat

Senior Member
I want to run 4 of these off the one chip..
Alan, as marzan will need 2 pnp drivers x 4 saa1064 ICs, .....
Hi Paul,

I believe marzan is only planning to use one SAA1064 so that driver ic looks rather unnecessary. It's not even particularly suitable (for minimising voltage drops) because it uses an NPN output stage which "throws away" more than a volt from the suppply rail (compared with a suitable PNP driver).

But IMHO this project really calls for a simple initial trial of one of the displays in the intended location (outdoors). The originally quoted 5 - 10 mA does seem rather "optimistic" for outdoor use, even with 11 volts across the display segments (there are obviously a number of LEDs in series). To test, it's only necessary to drive one segment via a few trial resistances from a known supply voltage (ideally measuring voltage and current with a multimeter) to see what current and voltage are actually required for the application.

Cheers, Alan.
 

marzan

Senior Member
But IMHO this project really calls for a simple initial trial of one of the displays in the intended location (outdoors). The originally quoted 5 - 10 mA does seem rather "optimistic" for outdoor use, even with 11 volts across the display segments (there are obviously a number of LEDs in series). To test, it's only necessary to drive one segment via a few trial resistances from a known supply voltage (ideally measuring voltage and current with a multimeter) to see what current and voltage are actually required for the application.
I have ordered some SAA1064`s as until now I have been using one of these :
http://www.ebay.com.au/itm/I2C-4-Digit-7-Seven-Segment-Display-Arduino-uController-5V-Choose-Red-Green-/230847256597?pt=LH_DefaultDomain_0&hash=item35bf919815
I wanted to see if I could understand the I2C side of things before jumping in the deep end and getting it working on a bigger display. Probably be a while until they get here, but it might be a good time to work on a schematic to breadboard to check if they are bright enough. Maybe some acrylic to sort out the contrast as well. Would I be better supplying a bit under the stated voltage rather than use current limiting resistors at his stage?
 

SAborn

Senior Member
If you need some SAA1064`s in a hurry let me know, as i have a tube of them here, then you can replace them when yours arrive.
 

marzan

Senior Member
If you need some SAA1064`s in a hurry let me know, as i have a tube of them here, then you can replace them when yours arrive.
Thanks for the offer SAborn. I have plenty to keep me busy until they arrive. I have a half built 5M trailer in the back yard, as well as a lot of equipment to grit blast and paint. thats the trouble with volunteering, they always keep you busy doing club stuff :)
 

AllyCat

Senior Member
Would I be better supplying a bit under the stated voltage rather than use current limiting resistors at his stage?
Hi,

No. Normally, I would always recommend using current-limiting resistors with LEDs (because they have a low internal resistance but a large "unknown" forward voltage drop) but the SAA1064 cathode drivers are constant current, so series resistors are not required (and the supply voltage largely irrelevant if within the appropriate range).

However, at least for testing, if you believe that the full drive of 21mA per segment might cause "issues", then it would be wise to include series resistors, or at least a fuse in the supply rail.

Cheers, Alan.
 

SAborn

Senior Member
Thanks for the offer SAborn. I have plenty to keep me busy until they arrive.
No Probs, There is often nothing worst than waiting a week or two for parts to proceed with a project, as there is a dozen or more chips here just thought i would make the offer.
 

PaulRB

Senior Member
I believe marzan is only planning to use one SAA1064 so that driver ic looks rather unnecessary. It's not even particularly suitable (for minimising voltage drops) because it uses an NPN output stage which "throws away" more than a volt from the suppply rail (compared with a suitable PNP driver).
My mistake, I misread marzan's earlier post and thought he needed 4 x saa1064 for 16 digits.

So that chip has npn drivers. Is there an equivalent to a ULN2803 but for high-side, with pnp drivers? (apologies for hijacking the thread with this question!)
 

AllyCat

Senior Member
Hi Paul,

Yes, possibly even "worse" because an NPN Darlington is shown (so at least two VBE voltage drops are "wasted"). But bear in mind these data sheets only show "functional diagrams" which may not indicate exactly what's on the chip.

I believe that it's still (more) difficult to make good/large PNPs in integrated circuits, so they're often constructed as a compound device with a PNP driving an NPN output. That adds a VBE voltage drop (~0.7 volt) but this may not be any worse than the voltage lost in the bulk resistance of a poorer PNP.

So, when/if it's important to minimise the high-side voltage drop, a discrete PNP transistor may be the best option.

Cheers, Alan.
 

marzan

Senior Member
While I was waiting for the main chip, I thought it might be a good time to sort out the schematic. After looking through the other posts , re-reading this one, and going over the datasheet of the SAA1064, I came up with this:

large I2C 7seg.png

Is it better to keep it all at 12v or run the chip at 5V?.
 

marzan

Senior Member
Just realized The 7segs run at 11v. So I guess I will have to drop the voltage somewhere on the schematic. What would be the best way to do that? Using a resistor to limit current wouldn`t work would it ? Because all the segments aren`t always on?

Marz.
 
Last edited:

AllyCat

Senior Member
So I guess I will have to drop the voltage somewhere on the schematic. What would be the best way to do that? Using a resistor to limit current wouldn`t work would it ? Because all the segments aren`t always on?Marz.
Hi,

Normally each LED/segment needs its own current limiting resistor, but the SAA1064 has internal current-limiting cathode drivers, so resistors (or the "correct" supply voltage) are not required. I don't think it matters much whether it's run from a 5v or 12v rail in that configuration, but the anode driver stages might need a little attention. I would put pull-down resistors (~10k) on pins 11 and 14 and increase R1 and R2 to perhaps 1k if the SAA rail is 12 volts (they're about correct for a 5 volt rail).

Cheers, Alan.
 

marzan

Senior Member
Thanks Allycat :)
Is this better? I want to make this part as a module so I can place it on a front panel. Can anyone see anything I might have missed?

I2C7seglarge.png

Thanks.
Marz.
 

AllyCat

Senior Member
Hi Marz,

That looks OK, but I am only working "theoretically", it's always best to test/breadboard if possible.

Do bear in mind that I2C is a short distance bus, the recommended <10cms might be a little pessimistic but do look at the Applications Note link and warnings in this recent thread.

Cheers, Alan.
 

SAborn

Senior Member
Nice work, looks great, also nice looking CNC mill you have built, it makes mine look crappy :(

Some nice red acrylic over the displays and it will look very professional, mind you if you do too good of a job there will be others beating on your door wanting to know if you will build them one.

There is a small local market for these large displays from various sporting groups with limited budgets, as i have been asked from a few over time to build them one, next time i will tell them "You are the Guru" and they can beat on your door and not mine.

Keep us posted with development updates of the project, its always nice to see the stages through to completion.
 

marzan

Senior Member
Nice work, looks great, also nice looking CNC mill you have built, it makes mine look crappy :(

Some nice red acrylic over the displays and it will look very professional, mind you if you do too good of a job there will be others beating on your door wanting to know if you will build them one.

There is a small local market for these large displays from various sporting groups with limited budgets, as i have been asked from a few over time to build them one, next time i will tell them "You are the Guru" and they can beat on your door and not mine.

Keep us posted with development updates of the project, its always nice to see the stages through to completion.
Thanks Pete. As you know the beauty of these mills is once you have the first one built it`s p&ss easy to do the next ones. I looked at purchasing this gear a while back and you get no change out of $2500, and it won`t have some the features I am hoping to implement (split timer, datalogging to laptop, barcoding database of competitors). cost so far? In money, less than a hundred bucks. The time you can`t really measure. Once I get the display built I need to work on the wireless part. I have some HT12-D`s and E`s and some of the cheapo jaycar 433mhz modules. I think the modules should be OK considering they will only have to work at an absolute maximum of 50m LOS.Add in a few infrared ttl modules and triple OR gates and it all should be good. Time will tell.

Marz.
 
Top