Port Config. - Can anyone help?

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Port Config. - Can anyone help?

Postby Strike_X » Fri Jan 14, 2011 9:44 pm

Hi there,

I'm totally new to PIC uC. However, I've learned some basic knowledge on assembly language (motorola-68K) before.

I'm going to use the PIC16F877A chip to design a stepper motor controller. There are 4 ports that I'm going to use as an input and output (digital) for my design.

PORTA - 2 bits input -RA0 & RA1
PORTB - 4 bits output - RB0, RB1, RB2, RB3
PORTC - 2 bits input - RC0, & RC1
PORTD - 4 bits output -RD0,RD1, RD2, RD3

below is part of my program.

CODE: SELECT_ALL_CODE
                                list p=16f877a
            include "p16f877a.inc"
            ERRORLEVEL -302 ;remove message about using proper bank

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

Index1   equ   b'0001'

                org 0x000             ;Reset vector
      goto Main

Main      
            bcf      STATUS,RP0
            bcf      STATUS,RP1
            clrf           PORTA
            bsf      STATUS,RP0
            movlw   0x06
            movwf   ADCON1
            movlw   0X03
            movwf   TRISA

            bcf      STATUS,RP0
            bcf      STATUS,RP1
            clrf           PORTB
            bsf      STATUS,RP0
            movlw   0x06
            movwf   ADCON1
            movlw   0X03
            movwf   TRISB

            bcf      STATUS,RP0
            bcf      STATUS,RP1
            clrf           PORTC
            bsf      STATUS,RP0
            movlw   0x06
            movwf   ADCON1
            movlw   0X00
            movwf   TRISC

            bcf      STATUS,RP0
            bcf      STATUS,RP1
            clrf           PORTD
            bsf      STATUS,RP0
            movlw   0x06
            movwf   ADCON1
            movlw   0X00
            movwf   TRISD

            movlw   Index1
            movwf   PORTB
            movlw   Index1
            movwf   PORTD

               end


So my question is:

(1) Am I configure the port correctly?

(2) As you can see that during the last 4 line of the program, I wanted to set the initial value of the PORTB and PORTD to become "0001" in binary. Therefore I expect to see 0x01h value when I run through the simulation by using MPLAB SIM right? But I notice that the value for PORTB & PORTD just doesn't changed and it stay 0x00h all the way during the simulation. Could any one tell me what actually went wrong here?

Thanks!
Strike_X
Freshie
 
Posts: 4
Joined: Fri Jan 14, 2011 6:47 pm

Re: Port Config. - Can anyone help?

Postby aurora » Sat Jan 15, 2011 1:41 am

Here you go:

list p=16f877a
include "p16f877a.inc"
ERRORLEVEL -302 ;remove message about using proper bank

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

Index1 equ b'0001'

org 0x000 ;Reset vector
goto Main

Main
bcf STATUS,RP0
bcf STATUS,RP1
clrf PORTA
bsf STATUS,RP0
movlw 0x06
movwf ADCON1
movlw 0X03
movwf TRISA

bcf STATUS,RP0
bcf STATUS,RP1
clrf PORTB
bsf STATUS,RP0
movlw 0x06
movwf ADCON1
movlw 0X03 <-- 0x00
movwf TRISB

bcf STATUS,RP0
bcf STATUS,RP1
clrf PORTC
bsf STATUS,RP0
movlw 0x06
movwf ADCON1
movlw 0X00
movwf TRISC

bcf STATUS,RP0
bcf STATUS,RP1
clrf PORTD
bsf STATUS,RP0
movlw 0x06
movwf ADCON1
movlw 0X00
movwf TRISD

bcf STATUS,RP0
movlw Index1
movwf PORTB
movlw Index1
movwf PORTD

end
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: Port Config. - Can anyone help?

Postby yonghui » Sat Jan 15, 2011 9:37 am

hi,

Assembly code.

for Port A, as input set the TRIS of corresponding bits to 1. configure the ADCONs for as digital input. check any other register or functions related to the port, in datasheet description of PORT,configure appropriately.

for outputs set the appropriate TRIS bit to 0s.

most of the time. more than one function is connected to the same PORT, so configuration of the port to multiplex it to the corresponding required function is needed for proper function

regards,
yh
thanks&regards,
yh
yonghui
Professional
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Port Config. - Can anyone help?

Postby Strike_X » Sat Jan 15, 2011 1:09 pm

Hi Aurora, Thanks:) It works now. At least I can see the expected value - 0X01h at the output PORT B & PORT D during the simulation.

Another question here. Since I'm using the PORT A & PORT C as my digital input, may I know is there any way that I can edit the value of the input PORT (PORTA&C) during simulation so that I can simulate the actual signal generation say RA0 & RA1 will be changed from 00 to 01 or 10 or 11 in binary ( 0X00h, 0X01h, 0X02h & 0X03h in hex)? I wanted to do this I as I need to verify the next part of my program where it involve some of the bit changes at the output site base on the input signal (digital) generated from the input PORT.

P.S - I'm using MPLAB SIM to check through the hex value of my program.

Thanks.
Strike_X
Freshie
 
Posts: 4
Joined: Fri Jan 14, 2011 6:47 pm

Re: Port Config. - Can anyone help?

Postby aurora » Sat Jan 15, 2011 5:19 pm

Yup, you can. Just change the PORTA/PORTC value in Watch window. By the way, I didn't check your code thoroughly (my assembly is bad, haha), but I think you are missing a few more lines there to make it work for reading PORTA/PORTC.
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: Port Config. - Can anyone help?

Postby Strike_X » Sun Jan 16, 2011 1:12 am

No. It just can't make any amendment for the input PORT (which is PORT A & C). Whenever I try to key in some value in the hex value field and press enter, the value will still turned back to 0X00. However, I notice that for others input field like output PORT (B & D), TRISA, B , C , D, STATUS and so on, I can simply key in any hex value. Is this normal?
Strike_X
Freshie
 
Posts: 4
Joined: Fri Jan 14, 2011 6:47 pm

Re: Port Config. - Can anyone help?

Postby aurora » Sun Jan 16, 2011 1:42 am

You need to change TRISA and TRISC to output mode (0x00) in order to change it. If TRIS is set to input mode, you can't change it. (Ya, I just figure that out too :p )

Anyway, have you consider using C? It's a lot easier.
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: Port Config. - Can anyone help?

Postby Strike_X » Sun Jan 16, 2011 2:57 am

I'm not familiar with C. Actually, I already write out the whole program of my design but when I tested it on the hardware side, the motor just doesn't move. Initially I thought there was something wrong with my hardware or it could be due to the oscillator problem, but when I get the port initialization right, I notice that the motor toque is there and I physically measure the output port - RC0, 4 point something volt was there. Meaning that the initial value that I set for output port is working. Should not have any problem for the hardware side.

Can you help on the program debugging or can any expert here help if I show u the whole part of my program?
Strike_X
Freshie
 
Posts: 4
Joined: Fri Jan 14, 2011 6:47 pm

Re: Port Config. - Can anyone help?

Postby aurora » Sun Jan 16, 2011 2:03 pm

Hmm... your problem now is that the uC does not process the input and give you the output you want?

How do you initialise input signal by the way? Post a small section of that code here. If you post everything, everyone will run away, including myself. :lol:

FYI, in C, the compiler will assign all the variables to the data memory automatically, so everything is invisible to us. In assembly, you need to do that yourself.
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron