Thursday, February 08, 2007

Such a long time...

A lot of electronics has been arriving in the post recently.

I like Farnell in the UK. So what if they're expensive, you can just click on stuff, and it turns up reliably at the door in a day or so. It's not quite the same in Malaysia. Not sure why, but Farnell here, like a lot of other businesses, doesn't accept credit cards online. I don't have a bank account in Malaysia, so I have to give the money to someone else, who has to drive into the nearest town to a Maybank (no other bank will do), deposit the money into Farnell's account. Then when I get the paying in slip, I take a photo of it with my digital camera. Then I upload the picture onto the PC. Then I resize it, sort out the shabby contrast. Then I email it to Farnell in Malaysia. When they see the email, then they process my order. It's a bit too much effort for the prices. They're still as good as gold at speedy deliveries, but the payment method...

Silicon Labs

I was looking for a small microcontroller. The package was the major consideration, I wanted something that had a footprint under 8mm x 8mm. Lots (more than 13) of I/O pins, plenty of which could be configured as interrupts. That was about it. Oh, and non-volatile program memory that could be updated in-system via a 2-wire interface. I spotted the Silicon Labs selection in the Farnell catalogue, but they didn't have the complete range, so the combination of small package and enough I/O was missing. www.silabs.com had plenty of suitable combinations though, and had online ordering from Mouser

Mouser.com

This is more like it! Clicking and buying. And the prices seem much better than Farnell too. But perhaps it's because there are no delivery charges included. Express delivery from the US is not cheap, but not outrageous either. One caveat is clearly presented on the website: the buyer takes repsonsibility for import charges. I had no idea what they could be, but had heard of 200% duty on foreign cars imported into Malaysia, so feared the worst. There didn't seem to be any definitive schedule on any of the gov.my sites. A few days later, the man on the scooter from GD Express showed up at the gate. The company phoned the day before to tell me what duty had to be paid - it was 10% - no big deal! I'll use mouser again.

C8051F530 TB

I have some prototyping boards with the C8051F530 processors on. They're very tidy. A USB dongle connects the host PC to the microcontroller's C2 (2-wire) debugging interface, and the UART is accessed via a USB adapter and SiLabs' CP2102 USB/serial device. A free IDE is available, and everything seemed to work straight out of the box. Except... could I get a simple program to write messages via the UART? I could not. If you've got this kit, watch out for the TX/RX - Chip revision gotcha. I had a Revision A chip (TX = P0.3, RX = P0.4), the board is connected correctly, but the labels on the UART header are for the Revision B chip (TX = P0.4, RX = P0.5). Hard to debug something, when you're looking at the wrong pins...

UART0

My worktop has been USB city recently, so my notebook (with Windows, for the SiLabs IDE) was all out of holes. I connected the USB-UART port to my Linux desktop and downloaded the binary (no source) USB UART driver from silabs.com. Rubbish. It didn't work. A version problem, I suspect. I noticed that there is a CP2101 driver in the kernel source, so I built that and loaded the module. Worked straight away! Used minicom and waited for the debug messages that never came. Maybe it's me, or my Linux setup, but the CP2101 USB UART kept disconnecting, and reappearing as a slightly different tty. No use at all for debugging, except in a Darwinian half-an-eye-better-than-no-eye sort of way. When it seemed to be working, I was sure the UART debug code wasn't, and when it wasn't, I had no way of knowing anything.

Crossbars, masks, watchdogs and clock dividers

I was beginning to doubt my ability to get this microcontroller working for me. It's an 8051 internally, but with an awful lot of extra stuff. I expect I'll find all this extra stuff useful eventually, but it was like a combination lock that prevented the new user (me) from getting over the "Hello, wold" hurdle. I lost days, sleep, skin tone, arguments etc over this. Why can't it be easy? There were example programs with the IDE: too simple (blinky.asm) for this micro, too out-of-date if they were UART code. I tried emailing the EU support (yes, I know, but you try emailing anybody's ASEAN support for anything). They had been quick and helpful when I was choosing which micro to buy. They didn't reply. I did feel a bit hopeless, with my "Hello, world" problem. It had worked on every other 8051-alike I'd tried. And I had RTFMed to discover the crossbar, port IO masks, clcok source register, and some other bits that were new.

Obviously, I should have read the whole manual first. The answers were all in there somewhere, but there was one note at the foot of one of the pages which really sorted out the problems for me. Just in case, in the unlikely event you're writing "Hello, world" on the F530, and you're dying the slow death of the ignorant programmer, here's my code. You'd buy me a drink for this, if I gave you this in person. Remember me kindly at Paypal, and I'll definitely drink to your success.















.module hellowld
; Hello world on UART, 9600n81
.include "../c8051f530.asm"
SYSCLK = 24500000
BAUDRATE = 9600

.area HOME (CODE)
sjmp _main

; -----------------------------------------
; function RS232_putchar
; char in accumulator
; -----------------------------------------
_RS232_putchar:

mov SBUF,a
00101$:
jbc TI,00108$
sjmp 00101$
00108$:
ret
; -----------------------------------------
; function RS232_putstr
; location of string in dptr
; uses accumulator
; -----------------------------------------
_RS232_putstr:
00101$:
clr a
movc a, @a+dptr
jz 00104$
inc dptr
lcall _RS232_putchar
sjmp 00101$
00104$:
ret
; -----------------------------------------
; function main
; -----------------------------------------
_main:
; Disable the WDT.
anl _PCA0CN, #~0x40 ; Stop the PCA first - per note foot of p206
anl _PCA0MD, #~0x40 ; clear Watchdog Enable bit
orl _OSCICN, #0x07 ; internal oscillator / 1
; Timer 1 mode 2, 8 bit auto reload
mov TMOD,#0x20
; reset timer control sfr
mov TCON,#0x00
mov _CKCON, #0x00 ; clock / 12 prescale
mov TH1, #0x96 ; 9600 baud
mov _P0MDOUT, #0x08 ; P0.3 push-pull output
orl _XBR0, #0x01; crossbar UART output enable
mov _XBR1, #0x40; and enable the crossbar
setb TR1; enable timer1 run control
clr TI; prepare Timer Interrupt flag
mov dptr, #__Hello_World
acall _RS232_putstr
loop_forever:
sjmp loop_forever
; ------------------------------------------
; constants etc
; ------------------------------------------
__Hello_World:
.ascii "Hello, world!"
.db 0x0d, 0x0a
.db 0x00