Friday, October 2, 2015

PIC 16F877 SPI porotocol using MPLAB IDE SPI write to 74HC164(SIPO).

Hi how are you guys doing.. 

This is the tutorial and code for the PIC 16F877,(MSSP moduel) SPI protocol in master mode interfaced with 74HC164( a serial in parallel out shift register). I think this code helps you guys in your project developments.  code works perfectly fine for any development. the clock frequency is Fosc/4.

Description of the code: the code below sends numbers from 0-254 to the 74HC164. If you want a detailed tutorial please comment below. (Code is self explanatory)

IDE Used: MPLABIDE and Proteus

SPI configuration for master mode:
CKE =0 ; SMP=0; CKP=1. 

Circuit Diagram: http://i61.tinypic.com/zx8swy.jpg


Source Code:

// PIC16F877 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = EXTRC     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

void delay();

void main()

    TRISC=0x00;
    PORTC=0x00;
   delay();delay();delay();
   delay();delay();delay();
   PORTC=0xff;
    TRISC |= 0<<3 | 0<<5| 1<<4;
    PORTC=
    SSPCON=0x00;
    SSPSTATbits.SMP=0;
    SSPSTATbits.CKE=0;
    SSPCONbits.CKP=1;
    TRISA=0x00;
    PORTAbits.RA5=1;
    ADCON0bits.ADON=0;     
    ADCON1 = 0x0F;                         
    INTCONbits.GIE=1;
    PIE1bits.SSPIE=1;
    PIR1bits.SSPIF=0;
    SSPCONbits.SSPEN=1;
    unsigned char p=0x00;
  
    TRISB=0x00;
    PORTB=0xff;
   
   
   
   
    while(p<254)
    {
      
       delay();
       delay();
       delay();
       delay();
       p++;
       SSPIF=0;
       PORTAbits.RA5=0;
       PORTB=p;
    
       SSPBUF=p;
       delay();
       delay();
       while(!SSPIF);
   delay();
       delay();
       delay();
       PORTAbits.RA5=1;
       delay();
       delay();
       delay();
      // SSPIF=0;
  
      
    }
   // Write your code here
   
}
 void delay()
 {
    unsigned int i;
    for(i=0;i<100;i++){}
    }

 void interrupt ISR()
 {
     SSPIF=0;
 }


 // please use the code, I am open sourcing.
//To say I developed it on my self, so just comment me if you have any issues with the code.

Wednesday, August 26, 2015

Simple explanation of linked list

// linked list code

#include <stdio.h>
#include <stdlib.h>
#define max 10


typedef struct list
{
int dat;
   struct list *next;

}list;


int main()
{    
     list a,b,c,d,e;  // list variables
    list *ptr;   // pointer of type list whihc points to list variables
     a.dat =5;
     a.next= &b;
     b.dat=4;
     b.next =&c;
     c.dat=3;
     c.next =&d;
     d.dat=3;
     d.next=NULL;
     ptr=&a;
 while(ptr!=NULL)
 {
     printf("value at location %d \n", ptr->dat);
     ptr=ptr->next;
 }


return 0;

}

Efficient stack implementation with pointers

 below is the code which implements a stack of size 10.  Pointer are used to easily push and pop the data. please feel free to use the code. can be used in the gcc compiler. Please feel free to use any functionality. change the max value and you can change it to what ever size stack you want...


 Source Code:
#include <stdio.h>
#include <stdlib.h>
#define max 10
int pos;
int *pop(int *pnt)
{
    int val=0;
    if(pos==0)
    {
        printf("no elements to delete ");
    }
    else
    {

    printf(" deleted value is %d ",*pnt);
    pnt--;
    pos--;
    }
    return pnt;

}
int push(int *pntr)
{
     int cntr;
    int val;

    if(pos==10)
    {
        printf("stack limit reached \n");
        printf("pop out a member first\n");
    }
    else{
        printf("enter the value to push \n");
        scanf("%d",&val);
        *pntr=val;
        pos=pos+1;
        printf("stack location %d \n",cntr);
    }

    return pos;
}
void ex()
{
    exit(0);

}




void prstck(int cnt,int *pt)
{
    int i;
    for(i=pos-1;i>=0;i--)
    {
        printf(" stack elem at locn  %d : %d  \n", i,*pt);
        pt--;
    }

}



int main()
{
int st[10];
int sel=0;
int cnt;
int *stp;      // stack pointer.
int *pppntr;

stp=st;

while(1)
{
  printf("enter your selection \n");
  printf("\n 1.push  2. pop  3. exit 4. display stack ");
  scanf("%d",&sel);

switch(sel)
{

    case 1 : stp++;
        cnt=push(stp);

             break;
    case 2 : pppntr= pop(stp);
             stp=pppntr;
             break;
    case 3 : ex();
             break;
    case 4 : prstck(cnt-1,stp);
             break;
    default : printf("no value entered \n");
                break;

}
}
return 0;
}















Thursday, August 20, 2015

ARM Development:: *** error 65: access violation at 0x40023800 : no 'read' permission --- FIX ARM

Hi,
Guess you are starting to learn the ARM development, and being a begineer in ARM,, getting this error is the most common issue... Its very easy to handle and it is fixable.. but you have to do it every time you start running the code in the keil simulator...


Before I recommend to view this page from ARM, you will get a basic idea what was that all about:

http://www.keil.com/support/docs/814.htm


So after reading this we know where the device MAP is but the question is what range of addresses I need to map:

Fro this we look at the error generated:

error 65: access violation at 0x40023800

Understanding the error:
From the documentation, form keil we understand that the address location pointed by the pointer in the RTOS, 0x40023800, is not available or the simulator cannot find the location. So it cannot perform read, write or execute anything at that location.

Resolve:
So now we try to map the range of addresses that the simulator want to access. For that , we see the error code and look for the address which is: 0x40023800.

Lets say we let it use the memory from 0x400000000 to  0x40FFFFFF, so that the location that the processor is trying to use is still in the range.

So we go to the

Debug-> Memory MAP
in the map range : 0x400000000,0x40FFFFFF
add these and select : read, write, execute.

and press run..