From: ian Date: Sat, 26 Nov 2005 13:33:28 +0000 (+0000) Subject: clock speed varies X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=a7f75088c3f97b9fbdad4df7f0cdc0ef68bb25db;p=trains.git clock speed varies --- diff --git a/cebpic/Makefile b/cebpic/Makefile index 6f418fc..0002a2a 100644 --- a/cebpic/Makefile +++ b/cebpic/Makefile @@ -3,8 +3,11 @@ CEBPIC= ./ PICNOS= 0 1 3 PROGRAMS= led-flash send-serial panic reply-serial \ - nmra-stream nmra-stream,slow tblrd_test \ - i2c-test i2c-test,slow + nmra-stream nmra-stream,slow + +# These programs haven't been updated to cope with +# master and slave pics having different clock speeds: +# i2c-test i2c-test,slow tblrd_test ROUTINES= routines-led LIBS= routines.lib diff --git a/cebpic/i2c-test.asm b/cebpic/i2c-test.asm index 8099e97..ee756ab 100644 --- a/cebpic/i2c-test.asm +++ b/cebpic/i2c-test.asm @@ -26,8 +26,8 @@ ; boilerplate: include /usr/share/gputils/header/p18f458.inc - include ../iwjpictest/clockvaries.inc radix dec + include ../iwjpictest/clockvaries.inc extern led_green extern led_red diff --git a/cebpic/led-flash.asm b/cebpic/led-flash.asm index 0a79e35..c6d3863 100644 --- a/cebpic/led-flash.asm +++ b/cebpic/led-flash.asm @@ -1,6 +1,9 @@ ; pin 21 (per-pic-led, RD2/PSP2/C1IN) states: high H = green, low L = red, float Z = black include /usr/share/gputils/header/p18f458.inc + radix dec + +clock equ mclock include ../iwjpictest/clockvaries.inc code @@ -8,9 +11,7 @@ start ; pin initial config bcf TRISE,4,0 ; turn off PSPMODE (Data Sheet p100/101) ; timer initial config - bcf T0CON,6,0 ; p107 Timer0 -> 16bit mode - bcf T0CON,5,0 ; timer0 use internal clock - bsc_morse_t0con_012 ; use prescaler? and configure it + morse_t0setup mclock, (1< 16bit mode - bcf T0CON,5,0 ; timer0 use internal clock - bsc_morse_t0con_012 ; use prescaler? and configure it + morse_t0setup mclock, (1< 16bit mode - bcf T0CON,5,0 ; timer0 use internal clock - bcf INTCON,5,0 ; clear TMR0IE => mask interrupt - bsc_morse_t0con_012 ; use prescaler? and configure it + morse_t0setup mclock, (1< 1.6us -morse_t0cycles equ 41250 ; * 1.6us = 66ms +morse_t0setup macro clock, t0con_other, lcount, hcount + local t0scale + local t0cycles + if clock==20000 ; Fosc=20MHz, Fcy=5MHz ie 200ns +t0scale equ 0011b ; 1:8 => 1.6us +t0cycles equ 41250 ; * 1.6us = 66ms endif - if mclock==1000 ; Fosc=1MHz ie 1us, Fcy=4us -morse_t0scale equ 1000b ; do not use prescaler -morse_t0cycles equ 16500 ; * 4us = 66ms + if clock==2000 ; Fosc=2MHz ie 500ns, Fcy=2us +t0scale equ 1000b ; do not use prescaler +t0cycles equ 33000 ; * 2us = 66ms + endif + if clock==1000 ; Fosc=1MHz ie 1us, Fcy=4us +t0scale equ 1000b ; do not use prescaler +t0cycles equ 16500 ; * 4us = 66ms + endif + movlw t0con_other | t0scale + movwf T0CON + if hcount>=0 + movlw (65535-t0cycles) / 256 + movwf hcount + endif + if lcount>=0 + movlw (65535-t0cycles) & 0xff + movwf hcount endif - -bsc_morse_t0con_012 macro - movf T0CON,0,0 - andlw 0xf0 - iorlw morse_t0scale - movwf T0CON,0 endm ;---------------------------------------------------------------------- ; busy-wait delay loop, originally from flasher.asm - if mclock==20000 ; each cycle 0.2us + if clock==20000 ; each cycle 0.2us delay_fastloop equ 1 ; each medium loop = ~40ms delay_slowexp equ 2 ; 2^2 * 40ms = 160ms endif - if mclock==1000 ; each cycle 4us + if clock==1000 ; each cycle 4us delay_fastloop equ 0 ; each medium loop = ~3ms delay_slowexp equ 6 ; 2^6 * 3ms = 192ms endif -define_busywait_delay macro -delay +implement_busywait_delay macro tclock ; we always leave each loop when its counter has reached 0, so on ; entry we assume it's already 0. For the loops which want to @@ -80,18 +91,32 @@ delay ; set a bit which says how fast the led ; should flash and count down from 2^(that bit) - bsf delay_countslow, delay_slowexp, 0 + if tclock >= 3915 + local fast=1 + local medium_cycles=198000 + else + local fast=0 + local medium_cycles=768 + endif + ; now 1/(medium_period in ms) + ; = tclock / (medium_cycles*4) + movlw 50 * tclock / medium_cycles ; 200ms + movwf delay_countslow, 0 + + local delayslow_loop + local delaymedium_loop delayslow_loop delaymedium_loop - if delay_fastloop + if fast + local delayfast_loop delayfast_loop decfsz delay_countfast, 1, 0 ; 1 cycle goto delayfast_loop ; 2 cycles (skipped or not) ; exited delayfast_loop ; total: 3 * 256 = 768 cycles endif - + decfsz delay_countmedium, 1, 0 ; 1 cycle goto delaymedium_loop ; 2 cycles (skipped or not) @@ -102,6 +127,11 @@ delayfast_loop goto delayslow_loop ; exited delayslow_loop + endm + +define_busywait_delay macro +delay + implement_busywait_delay clock return endm diff --git a/iwjpictest/flasher.asm b/iwjpictest/flasher.asm index 85e915b..0b86aa6 100644 --- a/iwjpictest/flasher.asm +++ b/iwjpictest/flasher.asm @@ -13,6 +13,7 @@ delay_countfast equ 0x00 delay_countmedium equ 0x01 delay_countslow equ 0x02 +clock equ mclock include clockvaries.inc code diff --git a/iwjpictest/harness.asm b/iwjpictest/harness.asm index eb25d2b..e7744d0 100644 --- a/iwjpictest/harness.asm +++ b/iwjpictest/harness.asm @@ -47,12 +47,13 @@ ; ; `printset' means print if `printset' flag is set, otherwise just evaluate - nolist include /usr/share/gputils/header/p18f458.inc + radix dec + +clock equ mclock include insn-aliases.inc include clockvaries.inc ; list - radix dec ;------------------------------------------------------------ ; variable declarations and RAM memory map diff --git a/pic.make b/pic.make index 6b8c7b6..6546ced 100644 --- a/pic.make +++ b/pic.make @@ -14,8 +14,8 @@ # FOO-entire0.hex FOO.o idlocs0.o config.o # perpicNUMBER.hex idlocsNUMBER.o config.o -ASFLAGS= -Dmclock=20000 -Dsclock=20000 -#ASFLAGS= -Dmclock=1000 -Dsclock=1000 +#ASFLAGS= -Dmclock=20000 -Dsclock=20000 +ASFLAGS= -Dmclock=20000 -Dsclock=2000 LINK= gplink -m -o $@ $^ ASSEMBLE= gpasm -p 18f458 $(ASFLAGS)