Sign In

PIC PROGRAMMING

fatma agsar fatma_agsar@hotmail.com Turkey

Hardware: JUST SIMULATION ON PROTEUS Software: ASSEMBLY LANGUAGE

I have to get data from Lm35 temparature sensor(ADC) and Ldr and show the data on Matlab command window(RS232).I try to do it in C and assembly C code is complete (except matlab part )but I get errors and in assembly I could not coplete it because I am a beginner.Could you please help me as soon as you can because I have 2 days to complete it.(Until thursday morning)

Images

PIC PROGRAMMING

Program

/*
 * File:   newmain.c
 * Author: z003en0j
 *
 * Created on 14 Haziran 2016 Sal?, 23:13
 */
#include <xc.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#define _XTAL_FREQ 4000000
#define LDR_PIN  RA0
#define LM35_PIN RA1

// CONFIG
#pragma config FOSC = EXTRC     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

void UART_Initialize()
{
    TRISC = 0b10000000;
    SPBRG=25;
    TXSTA=0b00100110;
    RCSTA=0b10010000;
    TXREG=0x00;
}

void UART_Write(char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text[i]!='';i++)
    UART_Write(text[i]);
}


void ADC_Initiaize()
{
    TRISA = 0xFF;    
    ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
    ADCON1 = 0xC0; //All pins as Analog Input
}


uint16_t ADC_Read(uint8_t channel)
{
  if(channel > 7) //If Invalid channel selected 
    return 0;     //Return 0

  ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
  ADCON0 |= channel<<3; //Setting the required Bits
  __delay_ms(2); //Acquisition time to charge hold capacitor
  GO_nDONE = 1; //Initializes A/D Conversion
  while(GO_nDONE); //Wait for A/D Conversion to complete
  return ((ADRESH<<8)+ADRESL); //Returns Result
}

                   IN C LANGUAGE 

void main(void) {
    uint16_t ldr_adc_val,lm35_adc_val;
    float lm35_temp,ldr_level;
    
    UART_Initialize();
    ADC_Initiaize();

    while(1)
    {
        char serial_text[16];
        ldr_adc_val=ADC_Read(0);
        ldr_level = ((ldr_adc_val*100.0)/1024.0);
        memset(serial_text,0,16);
        sprintf(serial_text,"ldr:%f ",ldr_level);
        UART_Write_Text((char*)serial_text);
        __delay_ms(500);
        
        lm35_adc_val=ADC_Read(1);
        lm35_temp = (5.0 * (float)lm35_adc_val * 100.0) / 1024.0;
        memset(serial_text,0,16);
        sprintf(serial_text,"lm35:%frn",lm35_temp);
        UART_Write_Text((char*)serial_text);
        __delay_ms(500);
    }
    return;
}


          IN ASSEMBLY
;; ANALOG READ AND DISPLY ON UART
;; ADC CHANNEL A0,A1

PROCESSOR 16F876A
INCLUDE <P16F876A.INC>

__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF

ANVAL	EQU	0x30 ;ANALOG VALUE OF AN0 INPUT
TXREG_T EQU 0x20
CNT0	EQU	0xb1 ;COUNTER IN BANK 00
CNT1	EQU	0xA1 ;COUNTER IN BANK 01

	ORG 0x00 ;WE MAKE USE OF INTERUPTS
INIT:
CLRF PORTC
	BSF STATUS, RP0 ;SELECT BANK 01

	;PORTA IS SET TO INPUT MODE
	MOVLW B'11111111'
	MOVWF TRISA

	CLRF TRISC ;SET ALL PINS OF PORTC AS OUTPUT	

	;---CONFIGURE SPBRG FOR DESIRED BAUD RATE
	MOVLW D'25' ;WE WILL USE 9600bps 
	MOVWF SPBRG ;BAUD AT 4MHZ

;---CONFIGURE TXSTA
	MOVLW B'00100100' ;CONFIGURE TXSTA AS :
	MOVWF TXSTA ;
	;8 BIT TRANSMISSION - 6.BIT
	;TRANSMIT ENABLED - 5.BIT
	;ASYNCHRONOUS MODE - 4.BIT
	;ENABLE HIGH SPEED BAUD RATE - 2.BIT

	;PORTB IS SET TO OUTPUT MODE
	MOVLW B'00000000'
	MOVWF TRISB

	BCF STATUS,RP0  ;SELECT BANK 00

	MOVLW B'10000000' ;ENABLE SERIAL PORT
	MOVWF RCSTA ;RECEIVE STATUS REG
	
	MOVLW '0'
	MOVWF TXREG_T ;TO TXREG

MAIN:
	CALL ADC_INIT ;INIT ADC MODULE
	CALL ADC_READ ;READ ANALOG VALUE AND STORE TO ANVAL REGISTER

   	MOVLW '1'
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW '-'
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVF ANVAL, W
	MOVWF PORTB
	MOVWF TXREG ;TO TXREG
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	CALL DELAY


	CALL ADC_INIT2 ;INIT ADC MODULE
	CALL ADC_READ ;READ ANALOG VALUE AND STORE TO ANVAL REGISTER
	
	
   	MOVLW '2'
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW '-'
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVF ANVAL, W
	MOVWF PORTB
	MOVWF TXREG ;TO TXREG
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	MOVLW ' '
	MOVWF TXREG ;TO TXREG
	CALL DELAY
	CALL DELAY
	CALL DELAY
	CALL DELAY
	CALL DELAY

	GOTO MAIN


;;;;ADC INIT PROC;;;;
ADC_INIT:
	BSF STATUS, 5 ;SELECT BANK 01

	;RIGHT JUSTIFIED RESULT, +VREF=VDD, -VREF=VSS, AN0-AN7=ANALOG
	MOVLW B'10000000'
	MOVWF ADCON1

	BCF STATUS, 5 ;SELECT BANK 00

	;FOSC/8, ADON=1
	MOVLW B'01000001'
	MOVWF ADCON0

	RETURN

ADC_INIT2:
	BSF STATUS, 5 ;SELECT BANK 01

	;RIGHT JUSTIFIED RESULT, +VREF=VDD, -VREF=VSS, AN0-AN7=ANALOG
	MOVLW B'10000000'
	MOVWF ADCON1

	BCF STATUS, 5 ;SELECT BANK 00

	;FOSC/8, ADON=1
	MOVLW B'01001001'
	MOVWF ADCON0

	RETURN

;;;;ADC READ PROC;;;;
ADC_READ:
	BCF STATUS, 5 ;SELECT BANK 00

	BSF ADCON0, 2 ;START CONVERTION PROCESS (WE SET THE GO BIT)
WAIT:
	BTFSC ADCON0, 2
	GOTO WAIT ;WAIT FOR CONVERTION TO FINISH (WAIT FOR GO BIT TO CLEAR)

	;;;WE SAVE RESULT INTO AN 8BIT REGISTER TO USE FOR OUTPUT (WE DROP THE TWO LESS SIGNIFICANT BITS);;;

	;FIRST WE PROCESS THE HIGH BYTE OF RESULT
	MOVLW 0x06
	MOVWF CNT0
AGAIN_0:
	BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
	RLF ADRESH, 1 ;SHIFT LEFT 6 BITS A/D RESULT HIGH BYTE
	DECFSZ CNT0
	GOTO AGAIN_0

	MOVF ADRESH, W ;MOVE ADRESH TO W
	MOVWF ANVAL ;MOVE W TO ANVAL


	;NOW WE PROCESS THE LOW BYTE OF RESULT
	BSF STATUS, 5 ;SELECT BANK 01
	MOVLW 0x02
	MOVWF CNT1
AGAIN_1:
	BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
	RRF ADRESL, 1 ;SHIFT RIGHT 2 BITS A/D RESULT LOW BYTE
	DECFSZ CNT1
	GOTO AGAIN_1

	MOVF ADRESL, W ;MOVE ADRESL TO W
	BCF STATUS, 5 ;SELECT BANK 00
	ADDWF ANVAL, 1 ;ADD W TO ANVAL

	RETURN

DELAY:
	MOVLW 0xF0
	MOVWF CNT1
D1:
	MOVLW 0xFA
	MOVWF CNT0
D0:
	DECFSZ CNT0
	GOTO D0
	
	DECFSZ CNT1
	GOTO D1

	RETURN
	END

I ADD PROTEUS SIMULATION  AND THE ERROR I GET IN C BELOW...

Replies

R Avinash
2016-06-21 11:49:05
If their are problems in compilation, then how did you ran the simulation?
R Avinash
2016-06-21 12:05:50
Is that you C program is OK, but you want to do the same in assembly?
R fatma agsar
2016-06-21 12:11:58
1-Hi avinash.I setup(I uninstall old version) my MPLAB IDE than I get errors.When I firstly run it It worked but now I get errors.
2-Yes I want it in Assembly and I need MATLAB part too
R Avinash
2016-06-21 12:24:29
As I can see, I think by mistake you are putting C program in an assembly file i.e. .ASM file. That is why it is not compiling ...
R fatma agsar
2016-06-21 12:32:42
How should I compile it? When I compile as .c I get error again
R Avinash
2016-06-21 12:43:14
what error you get when you compile as C file?

Please post screenshot.

May be you have forgot to install XC8 compiler ! It is installed separatly after MPLAB X IDE install.
R Avinash
2016-06-21 12:44:54
See this link for steps on creating C project using MPLABX

http://extremeelectronics.co.in/pic16f877a-tutorials/setup-and-use-of-lcd-library-for-pic/

Make exactly like that ...
R fatma agsar
2016-06-21 13:08:54
I get this error
R fatma agsar
2016-06-21 13:12:44
I use this Ide
R Avinash
2016-06-21 13:36:02
This IDE is old, please use MPLAB X IDE and XC8 compiler. Google to download them ...
R fatma agsar
2016-06-21 13:40:56
But we use this version at school.What is the difference between them I mean I need assembly codes and ıf you send in assembly will it work on this IDE? . I will need c version.I will download it.But for now firstly I have to do my project.
R Avinash
2016-06-21 13:53:22
You are creating a Assembly project by mistake. Please create a C project only!

Show me the screenshots of each step you are taking to create project!
R fatma agsar
2016-06-21 14:11:42
I am installing it now.But could you please firstly take a look to asm codes because I need ıi in asm not C
R Avinash
2016-06-21 14:16:29
sorry I cannot do assembly!
R fatma agsar
2016-06-21 14:23:48
I ınstall ccompiler but There is a problem I cannot see program file
R fatma agsar
2016-06-21 14:30:32
Okay did you compile my codes in assembly?What does problem might be?
R Avinash
2016-06-21 15:02:30
Did you installed MPLAB X IDE also ? or only XC8 compiler ?
R fatma agsar
2016-06-21 15:33:18
MPLAB IDE also
R Avinash
2016-06-21 15:43:31
Hmmmm.

Tommorow I will check it.

I think I need to use Team Viewer to help you.

So please keep it installed ...
R fatma agsar
2016-06-22 01:17:54
I install TeamViwer See you tomorrow
R fatma agsar
2016-06-22 08:30:28
Its 11:30 in my country now.I will be online again at 14:00.
R Avinash
2016-06-22 12:09:09
are you online? send details of your TeamViewer ...

Post a reply to fatma agsar

Think you can help fatma agsar ? Then post your thoughts that might help fatma agsar. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.