28X2 not running 18X program

jglenn

Senior Member
I have a program running on the 18X that uses the count instruction,
but it seems dead on a 28X2.

I am trying to diagnose the input to make sure it is working.

Puzzled that it flags this as a syntax error, because it is right out of the book:

let b1 = pins

Trying to used Debug to show pin 7, which should be input 7 (chip pin 18).

I have a LED on output 5. How can I get that pins line to work, or
how to write a program that will simply echo input 7 on out 5. A little
rusty as just got back into the PICAXE.

Also, I see no signal on the resonator pins, 9 and 10. It runs a LED
flasher program ok, so what gives? :confused: Have a real Rev Ed board.
 

eclectic

Moderator
First quick thought

let b1 = pinsC

e

And second thought

if pinc.7 = 1 then
high 5
endif
 
Last edited:

jglenn

Senior Member
I will try that next. This prog does not work:

main:
if pin7 = 1 then high 5 :endif
goto main

Input 7 IS high, but the led does not come on. Unless I change it to
if pin7 = 0. Do I have something scrambled?
 

eclectic

Moderator
I will try that next. This prog does not work:

main:
if pin7 = 1 then high 5 :endif
goto main

Input 7 IS high, but the led does not come on. Unless I change it to
if pin7 = 0. Do I have something scrambled?
If pinC.7 .....................
 

jglenn

Senior Member
Ran your second program, the led stays on all the time, even when

I put a low on input 7.

Edit: tried this, no go:

main:
if pinC.7 = 1 then high 5 :endif
goto main
 
Last edited:

jglenn

Senior Member
OK, duh, this works:

main:
if pinC.7 = 1 then high 5 :endif
if pinC.7 = 0 then low 5 : endif
goto main


Will get the code of that prog next I am having trouble with.
 

eclectic

Moderator
OR, this works as well

#picaxe 28x2


let dirsB = %11111111
let adcsetup = 0

main:
if pinc.7 = 1 then
high b.5
endif

pause 100
low b.5

goto main

e
 

jglenn

Senior Member
[main:
count 7,4000,w1

elvr: i2cslave %10011110,i2cslow,i2cbyte 'config i2c
writei2c (%11010000,w1,%00000000) 'ctrl byte, msb, lsb

debug
goto main
]

This is a program to count a 60 HZ signal on input 7, with debug I can
see 240 for b2, but on the 28X2 it is dead. I have to drive a D/A chip that is I2C. It works with the 18X. That runs a meter that says 60.0 HZ.

Can't get to work on the 28X2
 
Last edited:

jglenn

Senior Member
All of it is in between the brackets.

Here is a clue, I am counting over 4 sec, with the 18X, it takes 4 seconds
between Debug dumps. With the 28X2, it takes only 2 sec. It acts like
it is running at 8 mhz. But the led flasher program timing is right, one sec
is one sec.

Do you have to give it a command for the clock for 4 Mhz? I thought it
did that automatically. Why is count acting different with the 28X2? I have version 5.2.2 of the editor. Is that ok?????

Code:
main:
count      7,4000,w1

elvr:	i2cslave	%10011110,i2cslow,i2cbyte	'config i2c
	writei2c	(%11010000,w1,%00000000)	'ctrl byte, msb, lsb

debug
goto main
 
Last edited:

hippy

Ex-Staff (retired)
count 7, 4000, w1

The "7" is not a valid pin number for X2 - it should be expressed as port.number such as C.0, B.4

In reality pin "7" does exist, but it's probably not the pin you are using.
 

jglenn

Senior Member
Like this? Does not seem to help.

count C.7,4000,w1

I need the 28X2 to get the xtal/resonator. Never have used resonators,
on xtal you can see a little sinewave at 4Mhz. Are the res different?

And debug dumps are coming every 2 sec instead of 4 sec. Why?
 
Last edited:

hippy

Ex-Staff (retired)
Not sure why it's not working. Check the connection is to the correct leg and is of suitable voltage.

As to why the debug is coming out twice as expect, again I've no idea. If you post your full code we can look at it and maybe tell.
 

jglenn

Senior Member
Ah hah. Updated to latest prog ed from 5.2.2. Now works but am

getting 120 count instead of 240 on 18X. It seems to be running slow.
 
Last edited:

jglenn

Senior Member
Code:
main:
count      C.7,4000,w1

elvr:	i2cslave	%10011110,i2cslow,i2cbyte	'config i2c
	writei2c	(%11010000,w1,%00000000)	'ctrl byte, msb, lsb

debug
goto main
I got this to work by downloading the latest prog ed, instead of using 5.5.2.

But it reads 120 instead of 240, with the 18X.

Something SHOCKING, hippy. I suspected you might be programming the
28X2 for internal clock, did not understand why I cannot see the signal on
pin 9 or 10, like with a crystal. So I bent those 2 pins up. No resonator
in the circuit. The board still works!

Puzzling things..
 
Last edited:

hippy

Ex-Staff (retired)
As described in the manual, the COUNT period is in units of 1ms at 4MHz, units of 0.5ms at 8MHz; the X2's run at 8MHz. So the 4000 is a period of 4 seconds on an 18X, but 2seconds on an X2, hence you'll see half as many pulses. Either double the COUNT period or double the returned result to compensate.

The 28X2 can run from either internal or external oscillator. When run from internal, the XTAL pins are disconnected so nothing will be seen on those pins. When set to operate with an external oscillator but no resonator is present I believe operation will automatically fallback to internal operation.
 

jglenn

Senior Member
OK, I believe the X2 are running at 8 MHz. But I do not think the resonator is active. With xtals you can see a tiny sine wave on one of the leads, they can't be much different. It is hard to imagine the basic starting out with the internal clock, then checking to see if there is a resonator there! If not, when it goes to run with it, everything stops and it would get stuck.

There are 2 registers to set the OSC: OSCCON and FOSC. I am going to read about them and attempt to jam some values in there to select the ext osc.

Another thing that bugs me is page 167 of The Manual, setfreq. It says:

ON DEVICES WITH FIXED EXTERNAL RESONATORS THIS COMMAND CANNOT BE USED - you have to change the resonator.

That would imply my 28X2 is preset for the ext resonator, which I think I proved it is not.

Also, commands like setfreq k31 or setfreq em16 do not do anything, but
I guess they would not since you can't use setfreq with this chip, so why
are they listed? :confused:

Sorry for having problems, I chose this chip as a quick way to solve a problem with the Hertz display on a very expensive generator control. I already had the D/A running, and did not want to go thru that with a raw pic and assy code. Thanks for any help! :D
 

hippy

Ex-Staff (retired)
OK, I believe the X2 are running at 8 MHz. But I do not think the resonator is active.
Have you enabled the external resonator with a SETFREQ EMxx, and have you matched the EMxx value to the resonator frequency ?

Remember that on the 28X2 the times-four PLL is automatically enabled for the external oscillator. If you have an 8MHz resonator attached you should be using "SETFREQ EM32".

It is hard to imagine the basic starting out with the internal clock, then checking to see if there is a resonator there!
That's the way it works except on those PICAXE which require a permanent resonator or crystal to be attached. The PICAXE starts with an internal oscillator, is switched to use the external oscillator with the SETFREQ, and if the resonator isn't there, if the oscillator doesn't oscillate for whatever reason, the internal oscillator automatically takes over again.

For those which don't have the fail-safe fall-back, when switching to external oscillator, if it doesn't work, everything probably will stop dead.

If not, when it goes to run with it, everything stops and it would get stuck.
That is the case when for a PICAXE which requires a resonator to be permanently attached, and those which don't have a fail-safe oscillator capability.

There are 2 registers to set the OSC: OSCCON and FOSC. I am going to read about them and attempt to jam some values in there to select the ext osc.
You should not have to do anything beyond using the SETFREQ command.

Another thing that bugs me is page 167 of The Manual, setfreq. It says:

ON DEVICES WITH FIXED EXTERNAL RESONATORS THIS COMMAND CANNOT BE USED - you have to change the resonator.

That would imply my 28X2 is preset for the ext resonator, which I think I proved it is not.
"On devices with fixed external resonators", which means those which must have a resonator permanently attached to work; such as the older 28, 28A, 28X. That does not include the 28X1 or 28X2, nor other current PICAXE.

The 28X2 can use either the internal or external oscillator. When it starts up it will use the internal osciallator at 8MHz until you instruct it to use something else with SETFREQ.

Also, commands like setfreq k31 or setfreq em16 do not do anything, but I guess they would not since you can't use setfreq with this chip, so why are they listed? :confused:
The command is available for the 28X2 and all options should be functioning correctly.

The quickest way to prove that may be to have a SERTXD loop which will output at 9600 baud with the normal 8MHz. Add a SETFREQ, and as that changes, so too will be baud rate. If the message can be seen at 9600 baud, using "SETFREQ M4" will require the baud rate of the Terminal viewer to be halved to 4800 baud for the message to be readable.

Sorry for having problems, I chose this chip as a quick way to solve a problem with the Hertz display on a very expensive generator control.
It should be easy, no more than using the correct SETFREQ. Perhaps you should post your full code and full circuit diagram then we can see exactly what it is you are doing.

The only thing I can think is that you are selecting the external oscillator, there's a fault with the resonator or related hardware, so the chip is fail-safe dropping back to internal oscillator operation, or you've mis-matched resonator to SETFREQ setting. Neither of those should affect any ability to use "SETFREQ M4" or any other internal oscillator speeds though.
 

jglenn

Senior Member
You have cleared a lot of it up, the problem is that I did not know I need to
use setfreq. I have the 28 pin project board, the schem says the resonator is 4 mhz. That means I was running at 16 mhz? Odd, since the count command seemed to run at double speed over a stock 18X, which is 4 mhz.

I will try the setfreq commands again, the weekend is gone now.

The FSCM failsafe clock scheme is cool, on page 249 of the Microchip data sheet for the PIC18F2520.

I was hoping the reg config could be changed with peek/poke, but FOSC reg is in flash memory, which is NOT simple to change. I don't think it can be under basic. Not that you need to, just trying to understand this beast.

Really want to run at 4 mhz, so that means I need a 1 mhz crystal/reson?

Thanks for help. :)
 

hippy

Ex-Staff (retired)
I have the 28 pin project board, the schem says the resonator is 4 mhz. That means I was running at 16 mhz? Odd, since the count command seemed to run at double speed over a stock 18X, which is 4 mhz.
No, the resonator has no effect until a SETFREQ is issued. The 28X2 will run with an internal oscillator at 8MHz when it starts.

Really want to run at 4 mhz, so that means I need a 1 mhz crystal/reson?
Yes, you would need to use a 1MHz resonator to get 4MHz resonator controlled operation, but there is no "SETFREQ EM4" option provided and I do not know what the lower limit is for correct external oscillator operation.

You likely don't really want to run a 28X2 at 4MHz. A 28X2 operating at 8MHz has a comparable performance to an 18X running at 4MHz. If you reduced to 4MHz it will be considerably slower than the same on the 18X. You will also have to adjust pauses and so on to match the reduced speed. You are inviting more difficulties onto yourself.

You really need to adjust your COUNT command period rather than anything else.

If you really want 4MHz controlled operation you would be better off with a 28X1 rather than a 28X2. The 28X1 does not have internal PLL enabled so you can use an external 4MHz resonator plus "SETFREQ EM4".
 

jglenn

Senior Member
Just a couple more questions. Is the resonator on the 28pin board really
4 mhz? What setfreq command should I use to activate it? And will the
chip run at 8 or 16mhz when I do???? PIC says to use "parallel cut" crystals,
is there any list of good parts from digikey? Or resonator. I get the feeling resonators are cheaper, but xtals better. In the past I left off those 2 little 22pf caps and did not notice any change, what about that? It always started up. This was with PIC16C56. XT mode.

I know I am wasting the power of the X2 on this project, but the customer
is demanding xtal stability and is willing to pay for it. Is the 18X going away?
Seems like the 20X2 is more popular and cheaper, but still not xtal.
 

Technical

Technical Support
Staff member
The 28X2 will always run at 4x the rating of the external resonator/crystal, due to the internal PLL. So if you use a 4MHz resonator it will run at 16MHz.

So multiply your count time by 4 compared to 4MHz.

If you want maximum accuracy you must use a crystal with external caps, not a resonator.

However for your sample program/application you are really wasting your time, you won't realistically get any better accuracy than you already have!
 

hippy

Ex-Staff (retired)
Is the resonator on the 28pin board really 4 mhz?
The AXE020 is provided with a 4MHz 3-pin resonator; this should be marked on the resonator.

What setfreq command should I use to activate it? And will the chip run at 8 or 16mhz when I do????
For a 28, 28A and 28X there is no need to use a SETFREQ command. The PICAXE will run at the speed of the external resonator which will automatically be activated when the PICAXE powers on.

For the 28X1, to use the external resonator, a "SETFREQ EM4" should be used if a 4MHz resonator is fitted and the PICAXE will run at 4MHz, a "SETFREQ EM8" should be used if an 8MHz resonator is fitted and the PICAXE will run at 8MHz, a "SETFREQ EM16" should be used if a 16MHz resonator is fitted and the PICAXE will run at 16MHz.

For the 28X2, to use the external resonator, a "SETFREQ EM16" should be used if a 4MHz resonator is fitted and the PICAXE will run at 16MHz, a "SETFREQ EM32" should be used if an 8MHz resonator is fitted and the PICAXE will run at 32MHz.

I get the feeling resonators are cheaper, but xtals better. In the past I left off those 2 little 22pf caps and did not notice any change, what about that?
Resonators are usually cheaper but less accurate than crystals. Three pin resonators include the necessary capacitors built-in. Two-pin resonators and two-pin crystals should have appropriate capacitors fitted or oscillator frequency may not be correct.

I know I am wasting the power of the X2 on this project, but the customer is demanding xtal stability and is willing to pay for it.
I would recommend using a 28X1. Use "SETFREQ EM4" to use the provided 4MHz resonator and the PICAXE runs at 4MHz.

Alternatively, using a 28X2, use "SETFREQ EM16" to use the provided 4MHz resonator and the PICAXE runs at 16MHz. and adjust your program as appropriate to work correctly at 16MHz operation.
 
Last edited:

jglenn

Senior Member
Technical: With the original 18X freq meter, internal clk of course, the meter

(2V full scale analog LED digital, picked by customer, I tried to get this project to be digital, but failed), it will stay at 60.0 HZ unless I heat the board with a
hair dryer, or hit with freeze spray, then it will vary .1 HZ. Does not seem like
much, but the customer has demanded xtal clocking for stability. In the field
there are wild temp fluctuations, freq is very important in the generator industry.

Hippy: Will the 28X1 always be available, or are you phasing it out?

Thanks for all suggestions, looking at them now, and some I2C fun.

But can't I use the 28X2 with 4 mhz xtal, and setfreq EM16

for 16 mhz operation?


Also, some background, this hz indicator originally I made with a LM2907 which exhibited a lot of hard to explain drift, more than .3 Hz, and there is great political pressure to get it rite this time!
 
Last edited:

jglenn

Senior Member
This code is working, I need to generate a value of 240, which is then divided by 4 with a resistor divider and fed to the D/A and meter. Being high by .1 HZ is one millivolt! This is a stupid solution, but was forced into it. Should be pure digital.


I do now see a 3V fast sinewave on pin 10, the CLOCK. Cool.

Code:
main:

setfreq	em16

count      C.7,16000,w1

elvr:	i2cslave	%10011110,i2cslow,i2cbyte	'config i2c
	writei2c	(%11010000,w1,%00000000)	'ctrl byte, msb, lsb

debug
goto main
 

jglenn

Senior Member
Is there anything I have to do to get the I2C port to work? It seems dead.

This same code worked on the 18X. On the 28X it is pins 14 and 15 for SCL and SDA. I have 4.7K pullups to 5V. But both are sitting at about 3.5V, with

no pulse action. HELP.
.
.
I changed the one part to i2cslow_16

but it does not work. I don't have the D/A hooked up,
but still expect see a transmission on the scope.
 
Last edited:

hippy

Ex-Staff (retired)
I made a mistake earlier with suggesting the 28X2 would need its resonator changed to 8MHz and EM32 to be used. This came about while trying SETFREQ EM16 which threw a syntax error, I assumed that EM16 was not supported on 28X2 when it is. I suspect I typed #PICAXE 20X2 not #PICAXE 28X2. I can only apologise for that mistake.

So the correct information should have been, use the 28X2, keep the 4MHz resonator, use SETFREQ EM16 which will give 16MHz operating speed.

If code in post #26 works but in post #27 does not I suspect the difference is in not having the I2C device connected.
 
Last edited:

Technical

Technical Support
Staff member
As Hippy suggests, make sure the i2c hardware is correctly connected.

It is also bad practise to keep issuing the hi2csetup ( or older i2cslave) command in a loop on any chip. This should only be done once, right at the start of the program. Likewise for the setfreq command, no need for it to be in the loop either.
 

jglenn

Senior Member
Will implement that. There is a caution in one of the docs about using

i2cslave with the X2 chips. Is that absolute? It cannot work? The setup

command looks a lot more complex. I will play with this a little tonite. Hook
the D/A chip up. Had to get a new one, as the one I used with the 18X solution is no longer available. Our eng at work said I2C parts are generally compatible, unlike SPI parts. Comments?
 

hippy

Ex-Staff (retired)
Will implement that. There is a caution in one of the docs about using i2cslave with the X2 chips. Is that absolute? It cannot work?
Not sure - where's the caution given, or what does the caution say ?


The setup command looks a lot more complex.
No, it's actually near identical, for example these are the same -

I2CSLAVE $A0, I2CSLOW, I2CBYTE

HI2CSETUP I2CMASTER, $A0, I2CSLOW, I2CBYTE


Our eng at work said I2C parts are generally compatible, unlike SPI parts. Comments?
Generally true. Any I2C device should work on an I2C bus. With SPI there are a number of different standards an SPI device may conform to, some send data lsb first, some send msb first, some clock on rising edge, some on falling.

That doesn't mean there aren't some I2C devices which turn out to be problematic in some cases, but they are mostly a rarity.
 

jglenn

Senior Member
In the manual, pg 73, i2cslave, it says

For X1 and X2 parts the hi2setup command is recommended instead.

I can see the 28X2 trying to talk to the I2C D/a Chip, a AD5305 quad,
substituting for a DAC 5573 that is still working great with the 18X,

which I may be going back to soon. I set up the scope on normal trigger
and looked at the I2C clock and data lines, the old circuit shows lots of
activity, dozens of pulses on the clock, and data, less dense. Sweeping
at .2mS/div.

The 28X2 has a briefer xmission, 10 times less. I guess the chip is not
responding. I hoped addressing a different quad chip would work, maybe
the ID or address is different, have to look at tomorrow. The new D/A is
not producing an output.

Anyway, here is the improved code:

Code:
main:		i2cslave	%10011110,i2cslow_16,i2cbyte	'config i2c
		setfreq     em16	

main2:	count		C.7,16000,w1

		writei2c	(%11010000,w1,%00000000)	'ctrl byte, msb, lsb

debug
goto 		main2
 

hippy

Ex-Staff (retired)
Does the 28X2 work when used with "SETFREQ M8" ?

That would be the first thing to try. Second is to use the DAC and I2C commands you were using with the 18X. I would also try the new DAC with a modified 18X to verify it works with that.

There are too many changes ( PICAXE chip, operating speed, command constants, I2C device, I2C addressing ) to be able to tell what may be wrong or where the issue lies.

I would also recommend putting a reasonable PAUSE after your SETFREQ and I2CSLAVE to ensure everything has settled down, at least during program development. That PAUSE can be reduced or removed later once you have everything working.

The reason for recommending the new HI2C setup and commands is I believe because it's a good habit to get into now that PICAXE's are capable of being used as I2C slaves.
 

jglenn

Senior Member
Thanks for tips, have printed them out and will try. An update: Finally
read the data sheets of both D/A chips, quad 8 bit. What a surprise.

The old one uses 3 bytes on the I2C bus. Ctrl, MSB data, LSB data (null).

The new one uses 4 bytes: address, pointer, MSB, LSB. The address, enables,
R/W, and data bits are all scrambled between the 2 chips. And the bonus...

the new AD5305 has the 8 data bits in the middle of 2 data bytes! Yes, I guess
it could be formatted, but I don't really need a quad chip, just wanted for future expansion.

Tomorrow, will try to implement a single ch 8 bit D/A, on a SOT23-6. Has to be easier.. :eek:
 

jglenn

Senior Member
Finally got a D/A to work with the 28X2. The AD5301 in SOT23-6 pack.

8 bit, single channel. It uses a 3 byte I2C sequence like my old chip, I can
no longer get. A0 is 0. I am using this test program to send a byte to it,
I disabled the w1 link from the count statement measuring the 60 HZ line freq.

Code:
main:		i2cslave	%00011000,i2cslow_16,i2cbyte	'config i2c
		setfreq     em16	

main2:	'count		C.7,16000,w1    xxxxx

		writei2c	0,(%00011000,%00001010,%10100000) 'ADDR/MSB/LSB

debug
goto 		main2
I have a question on if it is possible to reformat some bits.

There is 3 bytes: ADDR, MSB, LSB.

At the end of the second byte, and the beginning of the third byte,
you see 1010, that is the 8 bit data. a7-a0, left to right.

How can I get w1 from my original count statement into these 2 targets?

The D/A is running, by altering these bit, I can get 0-5V. :cool:

The bits in the test program output 3.32V.
 
Last edited:

hippy

Ex-Staff (retired)
Changed

If I understand correctly, the value you read needs to be spread across the two bytes. In which case this would be a multiply by 16, shift left by four, then output the msb and lsb bytes of 'w2' separately ...

Count C.7, 16000, w2
w2 = w2 << 4
WriteI2c 0, ( %00011000, b5, b4 )
 
Last edited:

jglenn

Senior Member
Dudes, thanks, will be trying that code tomorrow. The Basic notation is not always simple to me, as I think in assembly terms. I looked in the manual for shift command, and only found the one for SPI. Where do they talk about >>?

I've already got 10 28X2 chips, this unit will not be super high quantity, but will sell a few dozen chips a year. Thinking of using the PICAXE for some items that benefit from user programmability.. :D
 

lbenson

Senior Member
>Where do they talk about >>?

Manual 2, page 20:

The X1 and X2 parts also support
<< ; shift left
>> ; shift right
*/ ; multiply (returns middle word of result)
DIG ; return the digit value
REV ; reverse a number of bits
 

jglenn

Senior Member
Excellent, I forgot about that important page.

Hippy, question about this line:

WriteI2c 0, ( %00011000, b5, b4 )

Why b4 and 5? Is that the parts of w2? Sorry for probable dumb question.
I understand that w variables are 16bit.
 

lbenson

Senior Member
Never done this before, so just working out what the chip is doing (thinking on paper).

Your value, %10101010 is decimal 170; 5 volts times 170 divided by range of 256 (# of values 0-255) equals 3.32 volts--your output.

<Lightbulb!>

So this is the reverse of ADC.

And I guess the odd placement of the 8 bits, spread across 2 bytes, is because other chips in this series provide 12-bit D-to-A, so these 8 bits are the most significant of a possible 12.
 
Top