![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
Hi,
By the way, my new problem is i saved my data (0x01) into eeprom address 0x01, when i recall the data it does not recall the same data but something different from it and sometimes i can save the data and recall correctly but sometimes not. What is the problem? is the setting or code problems? i already DOUBLExDouble check it in the DATASHEET for few hours and look for solutions on internet, i still cant recall correctly, PLEASe HELP.I write the data correctly...everytime, when i check in the EEDATA register b4 i reset the MPLAB SIM and rerun again. Code Below, PIC12F629 , WRITE code from me, ORIGINAL_PWM EQU 0x21 TEMP EQU 0x22 WRITE: BSF STATUS,5; switch to bank 1 MOVLW 0x01; go to addr 0x01 MOVWF EEADR; MOVF ORIGINAL_PWM,w ; (ORIGINAL_PWM = 0x01) MOVWF EEDATA; write 0x01 to EEDATA BSF EECON1,WREN; < follow the datasheet write process BCF INTCON,GIE ;< MOVLW H'0055' ;< MOVWF EECON2 ;< MOVLW H'00AA' ;< MOVWF EECON2 ;< BSF EECON1,WR ;< BSF INTCON,GIE ;< BCF STATUS,5; switch to bank 0 RETURN READ: BSF STATUS,5; switch to bank 1 MOVLW 0x01; goto addr 0x01 in eeprom MOVWF EEADR; BSF EECON1,RD ; read previously saved data 0x01 MOVF EEDATA,w ; move 0x01 to w MOVWF TEMP; (TEMP register ) move 0x01 to TEMP BCF STATUS,5; switch bank 0 RETURN First, Thanks a lot to view and helps. |
|
|
|
|
|
|
(permalink) |
|
Since it's only a code fragment I can't tell if you're waiting for the EEPROM to finish writing before reading it.
|
|
|
|
|
|
|
(permalink) |
|
***********************************************
#include <p12f629.inc> __CONFIG _CP_OFF & _MCLRE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _CPD_OFF &_PWRTE_OFF ORIGINAL_PWM EQU H'0025' TEMP EQU H'0022' ORG 0x000 BCF STATUS,RP0; CLRF GPIO; MOVLW 07h ; MOVWF CMCON ; BSF STATUS,RP0 ; CALL 3FFh; MOVWF OSCCAL; MOVLW H'0038'; MOVWF TRISIO; BCF STATUS,5; START BTFSS GPIO,3; wait for ON/OFF GOTO START; CALL READ ; Restore previous PWM RESTART_PWM MOVWF ORIGINAL_PWM; save EEDATA to register, for PWM cycle MOVWF TEMP; save to temporary counter BSF GPIO,0; signal to output 0, high cycle BSF GPIO,1; signal to output 1,high cycle BSF GPIO,2; signal to output 2,high cycle LOOP1 DECFSZ TEMP,1 ; PWM high cycle counter GOTO LOOP1 ; MOVLW d'255' ; 255 cycles MOVWF TEMP ; save for counter MOVF ORIGINAL_PWM,w ; SUBWF TEMP,1 ; BCF GPIO,0 ;low cycle BCF GPIO,1 ;low cycle BCF GPIO,2 ;low cycle LOOP2 DECFSZ TEMP,1 ; PWM low cycle counter GOTO LOOP2 ; BTFSS GPIO,3 ; check for on/off switch GOTO START ; BTFSC GPIO,4 ;check for PWM cycle increase CALL INCREASE ;increase PWM cycle BTFSC GPIO,5 ; check for PWM Cycle reduce CALL DECREASE ;decrease PWM cycle GOTO RESTART_PWM; repeat all hi/lo pwm cycle ***********SUBROUTINE************* READ: BSF STATUS,5; switch to bank 1 MOVLW 0x01; goto addr 0x01 in eeprom MOVWF EEADR; BSF EECON1,RD ; read previously saved PWM cycle MOVF EEDATA,w ; move saved data to w BCF STATUS,5; switch bank 0 RETURN WRITE: BSF STATUS,5; switch to bank 1 MOVLW 0x01; go to addr 0x01 MOVWF EEADR; MOVF ORIGINAL_PWM,w ; move new cycle to ORIGINAL_PWM MOVWF EEDATA; write 0x01 to EEDATA BSF EECON1,WREN; < follow the datasheet write process BCF INTCON,GIE ;< MOVLW H'0055' ;< MOVWF EECON2 ;< MOVLW H'00AA' ;< MOVWF EECON2 ;< BSF EECON1,WR ;< BSF INTCON,GIE ;< BCF STATUS,5; switch to bank 0 RETURN DECREASE: DECF ORIGINAL_PWM,0 ; decrease cycle by 1 MOVWF ORIGINAL_PWM; save to register CALL WRITE; GOTO RESTART_PWM ; RETURN INCREASE: INCF ORIGINAL_PWM,0 ; increase cycle by 1 MOVWF ORIGINAL_PWM; save to register CALL WRITE; GOTO RESTART_PWM ; RETURN END ************************************************** Above is my code...for normal PWM control LED dimmer. My main problem is i cant restore my saved PWM when i start up again. I simulated in MPLAB SIM ver 8 , the old PWM keep called. I want my saved PWM to be recalled when new start. I monitor each register, the EEDATA did save the PWM newly set, but when recall again at new start up, it will be the old 1. For example, my new PWM cycle is 0x05 and old PWM is 0x02 , after 0x05 was saved. then i send input to GPIO,3 to turn off the PWM and send a new input to turn on again, but it restored the old PWM data which is 0x02. Where is the problem? is it the config? or??? thanks |
|
|
|
|
|
|
(permalink) |
|
Here's a nice tip.
I took the liberty of neatening up your code, adding a proper cblock for the variables and changing the bsf/bcf bank changes to banksel's (and removed one unnecessary bank change). I also changed most of it to what is, in my opinion, much easier to read lower case. All upper case makes my eyes go wonky when I code. Everything looks the same. Code:
#include <p12f629.inc> __CONFIG _CP_OFF & _MCLRE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _CPD_OFF &_PWRTE_OFF cblock 0x20 original_pwm,temp endc org 0x000 clrf GPIO movlw 07h movwf CMCON banksel OSCCAL ;bank 1 call 3FFh movwf OSCCAL movlw H'0038' movwf TRISIO banksel GPIO ;bank 0 start btfss GPIO,3 ;wait for ON/OFF goto start call read ;Restore previous PWM restart_pwm movwf original_pwm ;save EEDATA to register, for PWM cycle movwf temp ;save to temporary counter bsf GPIO,0 ;signal to output 0, high cycle bsf GPIO,1 ;signal to output 1,high cycle bsf GPIO,2 ;signal to output 2,high cycle loop1 decfsz temp,1 ;PWM high cycle counter goto loop1 movlw d'255' ;255 cycles movwf temp ;save for counter movf original_pwm,w subwf temp,1 bcf GPIO,0 ;low cycle bcf GPIO,1 ;low cycle bcf GPIO,2 ;low cycle loop2 decfsz temp,1 ;PWM low cycle counter goto loop2 btfss GPIO,3 ;check for on/off switch goto start btfsc GPIO,4 ;check for PWM cycle increase call increase ;increase PWM cycle btfsc GPIO,5 ;check for PWM Cycle reduce call decrease ;decrease PWM cycle goto restart_pwm ;repeat all hi/lo pwm cycle ***********SUBROUTINE************* read banksel EEADR ;bank 1 movlw 0x01 ;goto addr 0x01 in eeprom movwf EEADR bsf EECON1,RD ;read previously saved PWM cycle movf EEDATA,w ;move saved data to w banksel GPIO ;bank 0 return write banksel EEADR ;bank 1 movlw 0x01 ;go to addr 0x01 movwf EEADR movf original_pwm,w ;move new cycle to original_pwm movwf EEDATA ;write 0x01 to EEDATA bsf EECON1,WREN ;< follow the datasheet write process bcf INTCON,GIE ;< movlw H'0055' ;< movwf EECON2 ;< movlw H'00AA' ;< movwf EECON2 ;< bsf EECON1,WR ;< bsf INTCON,GIE ;< banksel GPIO ;bank 0 return decrease decf original_pwm,0 ;decrease cycle by 1 movwf original_pwm ;save to register call write goto restart_pwm return increase incf original_pwm,0 ;increase cycle by 1 movwf original_pwm ;save to register call write goto restart_pwm return END
__________________
========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 17th May 2008 at 12:35 AM. |
|
|
|
|
|
|
(permalink) |
|
Here's another nice tip.
Code:
decrease decf original_pwm,0 ;decrease cycle by 1 Code:
increase incf original_pwm,0 ;increase cycle by 1 For example: Code:
decrease decf original_pwm,w ;decrease cycle by 1 Code:
increase incf original_pwm,w ;increase cycle by 1 Another thing I just noticed is this. In decrease and increase you're using decf to decrement the variable and storing the result in W (0 = W). Then you save W back to the variable. Code:
decrease decf original_pwm,0 ;or the equivalent, original_pwm,w movwf original_pwm ;save to register Code:
decrease decf original_pwm,f ;decrease cycle by 1
__________________
========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 18th May 2008 at 04:34 AM. |
|
|
|
|
|
|
(permalink) |
|
hi, thanks lots...
btw, can i know wat is the problem that cause my program did not read what it should be in MPLAM SIM? is it just because of my code repeatedly saved? ...still wondering what is the causes ... your help is very much appreaciated. thank you again.. i am still newbie to programming PIC... |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Keil C problem | sarang1_in | Micro Controllers | 3 | 27th February 2005 05:05 AM |
| Problem; Random pic performance | timothyjackson | Micro Controllers | 7 | 19th February 2005 06:19 PM |
| Big problem!! | zezito | Electronic Projects Design/Ideas/Reviews | 4 | 29th October 2004 02:05 AM |
| 12F675 Problem | brodin | Micro Controllers | 10 | 29th December 2003 09:40 PM |
| strange color camera problem | schrodingerscat | Electronic Projects Design/Ideas/Reviews | 5 | 4th October 2003 07:25 PM |