Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIC projects! C and Assembler
#1
PIC Projects!

Hi all,

These projects are designed on the PICKit2 programmer. They are for the PIC16F690 MCU.



Simple Hello World! -Assembler


Code:
list    p=16f690
    radix    dec
    include    "p16f690.inc"        
        
   __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF &      _MCLRE_OFF &      _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

;************************************
;Beginning of program
;************************************
    org    0x00
    bsf    STATUS, RP0     ;bank 1        
    bcf    TRISC,0    ;GP4 output        
    movlw   b'00001011' ;bit3=1=WDT  011=/8 WDT=18mSx8=0.144Sec
        movwf   OPTION_REG         ;must be in bank 1
        bcf    STATUS, 0    ;bank 0    
        movlw    b'00010000'    ;to toggle GP4
    xorwf    PORTC,4
    sleep
        
    END





Blinking LED: - Assembler


Code:
#include <p16F690.inc>
     errorlevel -302
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
       cblock  0x20
    d1      ; Define three file registers for the
    d2      ; delay loop
    d3
        endc

     org 0
Start:
     bsf     STATUS,RP0       ; select Register Page 1
     bcf     TRISC,0          ; make IO Pin C0 an output
     bcf     STATUS,RP0       ; back to Register Page 0
     bsf     PORTC,0          ; turn on LED C0
     call    Delay            ; delay so you can see the LED is on
     bcf     PORTC,0          ; turns off LED C0
     call    Delay            ; delay so Led will be off so you can see it blink
     goto    Start           ; wait here

Delay:
            ;499994 cycles
    movlw    0x03
    movwf    d1
    movlw    0x18
    movwf    d2
    movlw    0x02
    movwf    d3
Delay_0:
    decfsz    d1, f
    goto    $+2
    decfsz    d2, f
    goto    $+2
    decfsz    d3, f
    goto    Delay_0

            ;2 cycles
    goto    $+1

            ;4 cycles (including call)
    return

     end


The Happy birthday tune for a PIC! output pins are: RA4, PWR -Assembler

Code:
;**********************************************
;HAPPY BIRTHDAY TUNE                          *
;  11-5-2010                                  *
;                                             *
;**********************************************



    #include <p16F690.inc>
       __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF &      _MCLRE_OFF &      _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

;===============================
;
;       Configuration Bits
;
;===============================


note    equ    21h    ;value of HIGH and LOW for note
gap    equ    22h     ;gap between notes - uses "gap_1" delay
loops    equ    23h     ;loops of HIGH/LOW
temp1    equ    24h     ;temp file for note
jump    equ    25h
D1    equ    26h    ;used in 250mS delay
D2    equ    27h    ;used in 250mS delay
D3    equ    28h    ;used in 250mS delay
gapDela    equ    29h    ;used in gap delay
tempA    equ    2Ah    ;used in gap delay



;****************************************************************
;Beginning of program
;****************************************************************
org    0x00
    nop
    nop
    nop
    nop
    nop            
SetUp    bsf    STATUS, RP0     ;Bank 1        
    
           movlw    b'00001011'    ;Set TRIS  
    movwf    TRISA           ;RA4,4 outputs            
    bcf    STATUS, RP0    ;bank 0        
    movlw   07h             ;turn off Comparator
                  ;must be placed in bank 0
        clrf    jump        ;jump value for table        
    goto     M1        


;****************************************************************
;* Delays             *
;****************************************************************
    
        
    ;gap_1 produces gap between notes - 3rd byte in table
            
gap_1    movlw    .2
    movwf    gapDela
gap_1a    decfsz    tempA
    goto    gap_1a
    decfsz    gapDela,1 ;produces loops
    goto    gap_1a
    decfsz    gap,1    ;
    goto    gap_1
    retlw    00    

    
    ;250mS second delay
        
_250mS    nop
    goto    $+1        
    decfsz     D1,1
    goto     _250mS
    decfsz     D2,1
    goto     _250mS        
    retlw     00    
        

;********************************
;* Table            *
;********************************

table1    addwf   PCL,f   ;02h,1  add W to program counter
    nop        ;
    retlw   .100    ;loops - cycles of HIGH/LOW "G" -Hap
    retlw   .142     ;note - value of HIGH/LOW
        retlw   .50     ;gap between notes            
          
        retlw   .100    ;"G"-py
        retlw   .142
        retlw   .50        
        
        retlw   .220    ;"A"  birth
        retlw   .126
        retlw   .50                
      
        retlw   .200    ;"G"   day
        retlw   .142
        retlw   .50      
        
        retlw   .240    ;"C"   to
        retlw   .105    
        retlw   .50        
        
        retlw   .240    ;"B"   you
        retlw   .113
        retlw   .240        
        
    retlw   .97     ; "G" -Hap
    retlw   .142    
        retlw   .20              
          
        retlw   .97    ;"G"-py
        retlw   .142
        retlw   .20      
        
         retlw   .220    ;"A"  birth
        retlw   .126
        retlw   .20                
      
        retlw   .200    ;"G"   day
        retlw   .142
        retlw   .20        
        
        retlw   .240    ;"D"  to
        retlw   .94    
        retlw   .20        
        
        retlw   .240    ;"C"  you
        retlw   .105    
        retlw   .240        
        
        retlw   .97     ; "G"   -Hap
    retlw   .142    
        retlw   .20              
          
        retlw   .97    ;"G"  -py
        retlw   .142
        retlw   .20        
        
        retlw   .240    ;"G+"- BIRTH
        retlw   .71
        retlw   .1  
        
        retlw   .240    ;"G+"- BIRTH
        retlw   .71
        retlw   .20          
        
        retlw   .240    ;"E" -day
        retlw   .84
        retlw   .20          
              
        retlw   .240    ;"C"  
        retlw   .105    
        retlw   .20        
        
        retlw   .240    ;"B"
        retlw   .113
        retlw   .20
        
        retlw   .220    ;"A"  
        retlw   .126
        retlw   .240
                
        retlw   .240    ;"F"  
        retlw   .80
        retlw   .2
        
        retlw   .240    ;"F"  
        retlw   .80
        retlw   .2
        
        retlw   .240    ;"E"
        retlw   .84
        retlw   .2          
        
        retlw   .240    ;"C"  
        retlw   .105    
        retlw   .2        
          
        retlw   .240    ;"D"  
        retlw   .94    
        retlw   .2
        
        retlw   .240    ;"C"  
        retlw   .105    
        retlw   .2
                    
    retlw   0FFh
        

M1    call    _250mS
    call    _250mS
        
Main    incf    jump,1  ;increment pointer
    movf    jump,w        
    call    table1
    movwf    loops    ;first value from table    
    movlw    0ffh
    xorwf    loops,w    ;see if value is "0ffh"
    btfsc    STATUS,Z
    goto    SetUp            
    incf    jump,1
    movf    jump,w
    call    table1
    movwf    temp1    ;temp for note for HIGH/LOW
        
tune    movf    temp1,w
    movwf    note        
    bsf    PORTA,3
    bcf    PORTA,4
    goto    $+1
    goto    $+1
    goto    $+1
    decfsz    note,1
    goto    $-4        
    movf    temp1,w
    movwf    note        
    bcf    PORTA,3
    bsf    PORTA,4
    goto    $+1
    goto    $+1
    goto    $+1
    decfsz    note,1
    goto    $-4
    decfsz    loops,f
    goto    tune
    incf    jump,1 ;look for gap value
    movf    jump,w            
    call    table1
    movwf    gap
    bcf    PORTA,3    ;to produce silence
    bcf    PORTA,4    ;to produce silence
    call    gap_1    ;mS gap
    goto    Main
                
        
    END









Multi action LED sequencer! - C code

Code:
//include file
#include <pic.h>

  

#define PORTBIT(adr, bit)    ((unsigned)(&adr)*8+(bit))


static bit LED0 @ PORTBIT(PORTC, 0);
static bit LED1 @ PORTBIT(PORTC, 1);
static bit LED2 @ PORTBIT(PORTC, 2 );



unsigned int i;        //for loop pause
unsigned int c;        //for loop event loop

//pause functions
void pause_1();
void pause_2();

//main function
void main(void)
{    

    TRISC = 0x00;
    PORTC = 0b00000000;    
    
    while(1)
    {

        for(c=0; c<10; c++)
        {


            //forward
            LED0 = 1;
            pause_1();
        
            LED0 = 0;
            LED1 = 1;
            pause_1();
        
            LED1 = 0;
            LED2 = 1;
            pause_1();
        
            

            
        
            LED2 = 0;
            LED1 = 1;
            pause_1();
        
            LED1 = 0;
            LED0 = 1;
            pause_1();
            
        
        };

        //re-initalize PORTC
        PORTC = 0b00000000;
        
        for(c=0; c<10; c++)
        {

            PORTC = 0b10101010;
            pause_2();
            
            PORTC = 0b01010101;
            pause_2();
            
        };

        for(c=0; c<10; c++)
        {

            PORTC = 0b11110000;
            pause_2();

            PORTC = 0b00001111;
            pause_2();

        };

        for(c=0; c<10; c++)
        {

            PORTC = 0b11001100;
            pause_2();

            PORTC = 0b00110011;
            pause_2();

        };

        for(c=0; c<10; c++)
        {

            PORTC = 0b10000001;
            pause_1();

            PORTC = 0b01000010;
            pause_1();

            PORTC = 0b00100100;
            pause_1();

            PORTC = 0b00011000;
            pause_1();

            PORTC = 0b00100100;
            pause_1();

            PORTC = 0b01000010;
            pause_1();

        };

    }

}

void pause_1()
{

    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);

};

void pause_2()
{

    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);
    for(i=0; i<4000; i++);

};

Tell me if you want more!


The code was made by me, or taken from several projects on the web and modified to work with the PIC16f690


Inventor1
Reply
#2
Thanks, I will download that and check it out. But, for now, I'll stick to inline ASM @ C++. (Tongue)
Reply
#3
Pretty good post. I may be getting one of these soon so it's nice to see others active in such areas.
Reply
#4
(05-29-2011, 03:05 PM)The-One Wrote: Thanks, I will download that and check it out. But, for now, I'll stick to inline ASM @ C++. (Tongue)


Thanks!
I might be posting more soon if more people want them.


Inventor1
Reply
#5
How many people are in PICs and AVRs?
I'd like to see some Smile
Reply
#6
Ughh. This makes my head hurt. I couldn't read any of that lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)