Jump to content

Recommended Posts

I am new to the programming world so please be gentle!

 

I am currently in a class in college where I am learning how to program the Motorola 9S12 microcontroller. I am not having problems with this chip and am enjoying learning. I have a few small projects I would like to attempt for home and for work (eventually). I want to use a PIC but really don't know where to start to convert from one chip to the other.

 

I have looked at sample code and some things that I see I never had to do with the 9S12. For instance the #pragma statements that are at the top of most of those programs are foreign to me.

 

e.g.

 

#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

 

 

 

 

 

 

I am attaching a program that we had to do for our class that took the value off of the ADC and displayed it on an LCD. Can you guys look at this and tell me what all I would need to change to make this work on lets say a PIC16F913?

 

I know that reading and studying the PIC will help me more than anything and I am in the process of reading about the PIC16F913. I see the different port names and data direction registers and understand all of that so far..... well anyway here's my code. Thanks ahead of time guys!!

 

 

#include <stdio.h>

#define PORTA *(unsigned char volatile*)(0x00)

#define DDRA *(unsigned char volatile*)(0x02)

#define PORTK *(unsigned char volatile*)(0x32)

#define DDRK *(unsigned char volatile*)(0x33)

#define TSCR2 *(unsigned char volatile *)(0x4d)

#define TIOS *(unsigned char volatile *)(0x40)

#define TFLG1 *(unsigned char volatile *)(0x4e)

#define TC0 *(unsigned short volatile *)(0x50)

#define TSCR1 *(unsigned char volatile *)(0x46)

#define TCNT *(unsigned short volatile *)(0x44)

#define ATD0CTL2 *(unsigned char volatile *)(0x82)

#define ATD0CTL3 *(unsigned char volatile *)(0x83)

#define ATD0CTL4 *(unsigned char volatile *)(0x84)

#define ATD0CTL5 *(unsigned char volatile *)(0x85)

#define ATD0STAT0 *(unsigned short volatile *)(0x86)

#define ATD0DR0 *(unsigned short volatile *)(0x90)

#define ATD0DR1 *(unsigned short volatile *)(0x92)

#define ATD0DR2 *(unsigned short volatile *)(0x94)

#define ATD0DR3 *(unsigned short volatile *)(0x96)

 

/* Function Prototypes */

void delay_5ms(void);

void delay_20ms(void);

void instruction(void);

void data(void);

void ADC_CONVERT(void);

unsigned char line_1[]={" DC VOLT METER"};

main()

{

int i,s;

PORTA = 0x00;

DDRA = 0xff; // Set all PORTA bits as Outputs

PORTK = 0x00;

DDRK = 0x07; // Set PORTK bits 0-3 as Outputs

 

delay_20ms();

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

{

PORTA = 0x30; // Initialize Display

instruction();

}

PORTA = 0x38; // Interface Length = 8 bit, Display lines =2,

instruction(); // Character Font = 5x7

 

PORTA = 0x1c; // Move Cursor

instruction();

 

PORTA = 0x01; // Clear Display

instruction();

 

PORTA = 0x06; // Set Cursor Move Direction

instruction();

 

PORTA = 0x0c; // Turn Off Display Cursor

instruction();

 

while(line_1!= '\0')

{

PORTA = line_1;

data();

s++;

}

PORTA = 0x95; //Move cursor to line 3

instruction();

 

ATD0CTL2 = 0x80; //normal ATD operation

delay_5ms();

delay_5ms();

ATD0CTL3 = 0x20; //set for 4 conversions

ATD0CTL4 = 0x00; // 10-bit operation

 

while(1)

{

ADC_CONVERT();

delay_5ms;

PORTA = 0x95;

instruction();

}

 

}

void instruction()

{

PORTK = 0x04; // E=1, RS=0, R/W=0

delay_5ms();

PORTK = 0x00; // E=0

}

 

void data()

{

PORTK = 0x05; // E=1, RS=1

delay_5ms();

PORTK &= 0x0b; // E=0

}

 

void ADC_CONVERT(void)

{

unsigned int bin_sum;

unsigned int avg_bin_volts;

unsigned int int_volts;

unsigned int ones;

unsigned int tenths;

unsigned int hundreths;

unsigned char ones_char;

unsigned char tenths_char;

unsigned char hun_char;

 

ATD0CTL5 = 0x80;

while (!(ATD0STAT0 & 0x8000));

 

bin_sum = (ATD0DR0 + ATD0DR1 + ATD0DR2 + ATD0DR3);

avg_bin_volts = (bin_sum / 4);

 

int_volts = (100*avg_bin_volts/1023)*5;

 

ones = int_volts/100;

ones_char = (unsigned char)(ones + 48);

tenths = (int_volts - ones*100)/10;

tenths_char = (unsigned char)(tenths + 48);

hundreths = (int_volts - ones*100 - tenths*10)/1;

hun_char = (unsigned char)(hundreths + 48);

 

PORTA = ones_char; // Puts ones on LCD

data();

 

PORTA = 0x2E; // Puts decimal point on LCD

data();

 

PORTA = tenths_char; // Puts tenths on LCD

data();

 

PORTA = hun_char; // Puts hundreths on LCD

data();

 

PORTA = 0x56; // Puts V on LCD

data();

 

delay_5ms();

return;

}

 

 

void delay_5ms()

{

TSCR1=0x90; //enable TCNT and fast flags clear

TSCR2=0x03; //set prescale bits to 011

TIOS=0x01; //enable OC0

TC0=TCNT+15000; //start an OC0 operation with 5ms. delay

TFLG1=0x01; //clear OC0F flag

while (!(TFLG1 & 0x01));

return;

}

void delay_20ms()

{

TSCR1=0x90; //enable TCNT and fast flags clear

TSCR2=0x05; //set prescale bits to 101

TIOS=0x01; //enable OC0

TC0=TCNT+15038; //start an OC0 operation with 5ms. delay

TFLG1=0x01; //clear OC0F flag

while (!(TFLG1 & 0x01));

return;

}

Link to post
Share on other sites

I'll answer the first question but I dont have time to read through all the code.

 

 

This pragma tells the compiler to insert data into the config memory of the pIC. Config memory is inaccessable at run time and is used to define how the PIC should set up its hardware.

 

The settings you have are for

_PWRTE_OFF Powerup timer off

_BODEN_OFF Brown out detection off

_WDT_OFF Watchdog timer off

_LVP_ON Low voltage programming enabled

_CPD_OFF Something to do with code protection(cant remember)

_DEBUG_OFF Debug mode off

_HS_OSC Oscillator set to highspeed (4MHz or above )

_CP_OFF Something else to do with code protection

 

Refer to the data sheet for the PIC device you are using for details of what these do.

The boostc header file for your PIC will only have definitions for config bits for that device

 

You should refer to the BoostC manual as to using Pragmas

 

 

Two more things,

 

#include <stdio.h> is not required, you should include system.h as it brings in your processor header and defines certain builtin functions of BoostC.

 

#define PORTA *(unsigned char volatile*)(0x00)

 

This sort of stuff is defined in the appropriate header for the PIC you are using.

The header is included when you include system.h, the IDE selects the correct PIC header according to how you set the target device.

 

#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

 

Link to post
Share on other sites

Thanks! Sorry I didnt realize how much I was asking!! I have been reading the data sheet and am starting to see a few differences and how I need to change up for it.

 

When I get to the point of some exact questions I will post back up and ask those.

 

I'll answer the first question but I dont have time to read through all the code.

 

 

This pragma tells the compiler to insert data into the config memory of the pIC. Config memory is inaccessable at run time and is used to define how the PIC should set up its hardware.

 

The settings you have are for

_PWRTE_OFF    Powerup timer off

_BODEN_OFF  Brown out detection off

_WDT_OFF      Watchdog timer off

_LVP_ON        Low voltage programming enabled

_CPD_OFF      Something to do with code protection(cant remember)

_DEBUG_OFF  Debug mode off

_HS_OSC        Oscillator set to highspeed (4MHz or above )

_CP_OFF        Something else to do with code protection

 

Refer to the data sheet for the PIC device you are using for details of what these do.

The boostc header file for your PIC will only have definitions for config bits for that device

 

You should refer to the BoostC manual as to using Pragmas

 

 

Two more things,

 

#include <stdio.h> is not required, you should include system.h as it brings in your processor header and defines certain builtin functions of BoostC.

 

#define PORTA *(unsigned char volatile*)(0x00)

 

This sort of stuff is defined in the appropriate header for the PIC you are using.

The header is included when you include system.h, the IDE selects the correct PIC header according to how you set the target device.

 

 

#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

 

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...