PIC12F675 ADC to se...
 
Notifications
Clear all

PIC12F675 ADC to seed random

15 Posts
8 Users
0 Reactions
9,763 Views
Materdaddy
Posts: 2366
Topic starter
(@materdaddy)
Famed Member
Joined: 16 years ago
[#21613]

I'm planning to use a DIGWDF 675 board to randomize my strobes that don't like to randomize otherwise, so instead of the simple loops to repeat a chase or something, I'm getting a little ambitious and I want to randomly turn on the outputs and at a random delay.

My plan is to do something like this (pseudo code):

1. set things up on the chip
2. seed random generator by reading the ADC (on an unused pin)
3. call random function
4. delay lengh in W
5. call random function
6. turn on pin W%4
7. Go back to 3 and repeat

I have most of this coded up (or so I think)...

I have a subroutine for random (found on the internet) that works depending on a random seeded value. It seems to work, although it always produces the same "random" since I always seed it by loading a w directly with a value.
I wrote a delay subroutine that works.
I wrote a routine to turn on one of 4 pins. I have tested this by loading w directly.

I changed my code to attempt reading the ADC and now the PIC doesn't work. I'm sure I probably missed something, but I wondered if anybody else is doing some more "advanced" things with the 675 and wanted to take a look at my code. I've attached the MPLabX project to this post.

firmware.asm - main code & program
blink.asm - not compiled, but this was used to test that I was getting random values, and counters/loops were working properly. Load something to w, call blink, and an LED on pin2? should blink that many times.
delay10.asm - subroutine to delay in 10ms per decimal value in w.
random.asm - the psuedo random subroutine I found on the tubes. It names a gp register RANDOM that is used for seeding... my firmaware.asm attempts to read the ADC and seed the RANDOM register before I use this, but when I made these changes, I broke everything. I probably have a mux, or some other register bit set incorrectly.
turn_on.asm - defines my turn-on subroutine that will only look at the first 4 decimal values and turn on the corresponding pin for my purposes. I'm using GPIO5 for 1, GPIO4 for 2, GPIO1 for 3, and GPIO0 for 4. This is due to the layout of the 675 board from Dirk and leaving GPIO3 available as ADC input.


14 Replies
Posts: 2167
(@djulien)
Noble Member
Joined: 19 years ago
Materdaddy wrote:I changed my code to attempt reading the ADC and now the PIC doesn't work.

Do you have a copy of the code before that change that you can compare to?

Why are the "banksel" lines commented out in firmware.asm? I think you would need those since ANSEL and TRISIO are in a different bank than GPIO, ADCON0 and CMCON.

don


Reply
Materdaddy
Posts: 2366
Topic starter
(@materdaddy)
Famed Member
Joined: 16 years ago

Unfortunately I don't have a copy. I'm a linux guy, so whenever I do anything in windows, I'm out of my comfort zone. If I had been in linux, I would because I'd have tracked it in git.

From my understanding of the 675, there are not separate banks and they aren't needed. I believe they were giving me warnings which is why I commented them out. I've only done a little assembly ever, and this is my first attempt at PIC code, so please, point out all obvious things. I'll try uncommenting those to see if it at least gets me back to a usable state.


Reply
Posts: 83
(@damo1271)
Estimable Member
Joined: 16 years ago

I think the banksels are needed to select the proper register banks. You also need to make sure that you have the correct bank sleected when you enter into your subroutines as these use registers that are in bank 0.
For example, you banksel loopct for example, which may be in bank 1, then go on later to work with GPIO which is in bank 0.

If it were me i would define my variables in bank 0 so that I only had to work with bank1 during the initialisation.

I am not sure of your intent, but there also seems to be a problem in your loop forever - it does nothing so once the program has run once it sits and does nothing. Is that the intent?
My PIC coding is a bit rusty so I stand to be corrected on any of the above. I hope this gives a little direction for you to investigate further.


Reply
ebrady
Posts: 670
(@ebrady)
Prominent Member
Joined: 17 years ago

Download the datasheet at http://ww1.microchip.com/downloads/en/DeviceDoc/41190G.pdf

Page 10 shows the memory map, there are two register banks for the PICF12675, so the banksels should be uncommented anytime you access a variable. The banksels for the xxxxx_R function calls are not needed and will generate warnings.

In main_loop, the following code does not appear to do anything, so I commented it out.

movlw .1
movlw .2
movlw .3
movlw .4

I took a quick look at firmware.asm and made a few changes.

See if this helps, I have not looked at any of the other files due to being out of time, so there may be additional changes needed also.

;******************************************************************************
;   This file is a basic code template for object module code                 *
;   generation on the PIC12F675. This file contains the                       *
;   basic code building blocks to build upon.                                 *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on             *
;   features of the assembler and linker (Document DS33014).                  *
;                                                                             *
;   Refer to the respective PIC data sheet for additional                     *
;   information on the instruction set.                                       *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:      xxx.asm                                                   *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files required: P12F675.INC                                              *
;                                                                             *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=12F675              ; list directive to define processor
     #INCLUDE <P12F675.INC>          ; processor specific variable definitions

     ; Functions from other files
     EXTERN     delay10_R
     EXTERN     blink_R
     EXTERN     turn_on_R
     EXTERN     random_R

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the 
; .asm file. The lables following the directive are located in the respective 
; .inc file.  See the data sheet for additional information on configuration 
; word settings.
;
;------------------------------------------------------------------------------

    __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

;------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;------------------------------------------------------------------------------

; example of using Shared Uninitialized Data Section
            UDATA_SHR
sGPIO       RES     1       ; Shadow register for GPIOs
            GLOBAL  sGPIO   ; Make it accessible elsewhere
RANDOM      RES     1
            GLOBAL  RANDOM

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RESET   CODE    0x0000  ; processor reset vector
        GOTO    START         ; go to beginning of program

;------------------------------------------------------------------------------
; SUBROUTINE VECTORS
;------------------------------------------------------------------------------
delay10
        ;pagesel delay10_R
        goto    delay10_R
        GLOBAL  delay10 ; Make this global so others can use the vectors

blink
        ;pagesel blink_R
        goto    blink_R

turn_on
        ;pagesel turn_on_R
        goto    turn_on_R

random
        ;pagesel random_R
        goto    random_R

;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

MAIN_PROG   CODE
START

;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE
;------------------------------------------------------------------------------

        errorlevel -302
        movlw   b'00001100' ; set everything to output (except GP3)
        banksel TRISIO
        movwf   TRISIO

        ;clrf    ANSEL
        movlw   b'00000100' ; disable digital input buffer (set AN2 it to analog)
        banksel ANSEL
        movwf   ANSEL

        movlw   b'10001000' ; set right justified and select AN2 to be read into ADRESH
        banksel ADCON0
        movwf   ADCON0

        movlw	b'00000011'	; Comparator configuration
        banksel CMCON
        movwf	CMCON

        movlw   b'00111111' ; make them all high (LEDs wired to light when pin is low)
        movwf   GPIO
        movwf   sGPIO
        errorlevel +302

seed_random
        bsf     ADCON0,GO       ;Start A/D conversion
wait
        btfsc   ADCON0,GO       ;Wait for conversion to complete
        goto    wait
        movf    ADRESH,w        ;save analog read to w
        movwf   RANDOM          ;write A/D result to PORTC

main_loop

        call    random
        call    delay10
        call    random
        ;movlw   .1
        ;movlw   .2
        ;movlw   .3
        ;movlw   .4
        movf    RANDOM,w
        call    turn_on
        goto    main_loop

        END
Materdaddy wrote:Unfortunately I don't have a copy. I'm a linux guy, so whenever I do anything in windows, I'm out of my comfort zone. If I had been in linux, I would because I'd have tracked it in git.

From my understanding of the 675, there are not separate banks and they aren't needed. I believe they were giving me warnings which is why I commented them out. I've only done a little assembly ever, and this is my first attempt at PIC code, so please, point out all obvious things. I'll try uncommenting those to see if it at least gets me back to a usable state.


Reply
Materdaddy
Posts: 2366
Topic starter
(@materdaddy)
Famed Member
Joined: 16 years ago
damo1271 wrote:I think the banksels are needed to select the proper register banks. You also need to make sure that you have the correct bank sleected when you enter into your subroutines as these use registers that are in bank 0.
For example, you banksel loopct for example, which may be in bank 1, then go on later to work with GPIO which is in bank 0.

If it were me i would define my variables in bank 0 so that I only had to work with bank1 during the initialisation.

I am not sure of your intent, but there also seems to be a problem in your loop forever - it does nothing so once the program has run once it sits and does nothing. Is that the intent?
My PIC coding is a bit rusty so I stand to be corrected on any of the above. I hope this gives a little direction for you to investigate further.

You're correct. I meant to only comment out pagesel since there's only 1 page worth of program memory, but also commented out banksel for named registers. I didn't realize that's what Don was previously talking about. I've now uncommented the banksel instructions.

The loop forever is there for my testing. Right now I want it to turn on 1 LED and stop (loop forever). Once I have things working, I'll have it goto "main_loop" so it'll keep generating random numbers and "turning on" an output (even if it's already on). That's the final intended use.

ebrady wrote:Download the datasheet at http://ww1.microchip.com/downloads/en/DeviceDoc/41190G.pdf

Page 10 shows the memory map, there are two register banks for the PICF12675, so the banksels should be uncommented anytime you access a variable. The banksels for the xxxxx_R function calls are not needed and will generate warnings.

In main_loop, the following code does not appear to do anything, so I commented it out.

movlw .1
movlw .2
movlw .3
movlw .4

I took a quick look at firmware.asm and made a few changes.

See if this helps, I have not looked at any of the other files due to being out of time, so there may be additional changes needed also.

I commented out those lines loading decimal 1-4 because they were there to test my "turn_on" routine. I would uncomment one at a time to test that.

Besides getting the banksel instructions back, your changes appear to be what I will want to use once ADC and RANDOM are working. With your changes, unfortunately nothing happens. I have tested (commented out in the original version I uploaded) that loading something into RANDOM and calling the random subroutine gives me values, but once I removed that and tried reading the ADC into random, I'm either getting 0, or the code spins forever or something strange since I never get any output turning on. Thanks for the suggestions and help.


Reply
Posts: 2167
(@djulien)
Noble Member
Joined: 19 years ago

Btw, another option for seeding the random number generator would be to write the last-used value into EEPROM, and then read and update that the next time the PIC is powered up. Then you would not need any external circuitry for the ADC.

for example:


powerup:
    read previous value from EEPROM
    generate next seed value
    write new seed value into EEPROM

Basically, you would be using one long, continuous random number loop rather than one shorter one that restarted each time you powered up.

don


Reply
Materdaddy
Posts: 2366
Topic starter
(@materdaddy)
Famed Member
Joined: 16 years ago

Interesting approach. I hadn't heard of that and I like it. My only concern is the number of writes to eeprom these are rated for. Do you know what that is? My use for these is to randomize my strobes. They'll probably be turned on at least once per song. Back-of-the-envelope style, that's probably about 80-100 times per night for 30 nights for a couple thousand read/writes total. Are these rated for millions of writes or would I have to replace my 675s every year? Maybe I could just reprogram them each year to read/write to a different offset of EEPROM?

After a little more tinkering I think the ADC read is seeding with 0 and that's giving a random number of 0. I'm using a "chip clip" on a breadboard and it looks like MCLR is the line connected to that pin, I'm thinking maybe I'm reading a 0 since I think it's low (not floating) when the pickit is running my circuit.


Reply
dirknerkle
Posts: 10293
(@dirknerkle)
Illustrious Member
Joined: 18 years ago

Data sheet says 1 million write eeprom endurance... 100K write flash endurance. But the chip is cheap enough you could replace it every year, too.


Reply
Posts: 6720
(@p-short)
Illustrious Member
Joined: 20 years ago

If you're going to use the EEPROM for the randomizing, I'd re-write it once at power-up (perhaps using one LFSR to generate a new seed for the next power-up), and use another LFSR for generating the random numbers after reading (and updating) the seed. I don't see any benefit to continually updating the seed in the EEPROM.


Reply
Page 1 / 2
Share: