CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC24/dsPIC® Compiler Comments
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








PostPosted: Sun Jan 20, 2008 10:59 am     Reply with quote

Quote:
Hello! I'm using PCWHD 4.057 and I have a problem with a 30f3011.
I see in the datasheet that this pic has three external interrupts, but in the compiler it looks like that support for INT_EXT1 is missing, while EXT0 and EXT2 are working fine. It's missing in the 30f3011.h file and even when I added in it, the compiler still saying invalid preprocessor directive or something like that.
Is it some way to correct this problem? Thanks!


Yes...upgrade to a more legit version ;) 4.057 is common everywhere and is very boggus ... consider buying PCD and upgrading to 4.066
Bob Sacamano



Joined: 17 Jan 2008
Posts: 16
Location: Somewhere Cold, USA

View user's profile Send private message

PostPosted: Mon Jan 21, 2008 9:27 am     Reply with quote

Quote:
It's missing in the 30f3011.h file and even when I added in it

Adding the header file does not add support for a chip. The support for a chip is built in to the compiler. To get support for a particular chip, you must upgrade to a version of the compiler that supports this chip.
_________________
"And you want to be my latex salesman."
Matthew Aurand



Joined: 20 Dec 2007
Posts: 9

View user's profile Send private message

External Clock Setup Questions
PostPosted: Wed Feb 06, 2008 6:39 pm     Reply with quote

Hi all,

I am trying to setup my external 40MHz clock on a dsPIC33FJ64MC706 and having issues. I want to get as many MIPS with this as possible from this clock.

The setup_oscillator constants are minimal in the device.h file:
Code:

// Constants used in setup_oscillator() are:
#define OSC_INTERNAL     32
#define OSC_CRYSTAL       1
#define OSC_CLOCK         2
#define OSC_RC            3
#define OSC_SECONDARY    16


My fuses:
Code:

#include <33FJ64MC706.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES ICS0                     //ICD communication channel 0
#FUSES JTAG                     //JTAG enabled
#FUSES NOCOE                    //Device will reset into operational mode
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES EC                       //External clock with CLKOUT
#FUSES OSCIO                    //OSC2 is clock output
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES TEMP                     //Temparature protection enabled
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NOSSS                    //No secure segment
#FUSES NORSS                    //No secure segment RAM
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NORBS                    //No Boot RAM defined
#FUSES NOPWMPIN                 //PWM outputs drive active state upon Reset

#use delay(clock=40000000)


And finally my test delay code:
Code:

void main()

   setup_oscillator(OSC_CLOCK,0);
   setup_wdt(WDT_OFF);
   setup_timer1(TMR_DISABLED);
   set_tris_b(255);
   #use fast_io(B)
   while(TRUE){
      output_high(F_RED);
      delay_ms(200);
      output_low(F_RED);
      delay_ms(200);
   }
}


Rather than the delay_ms functions delaying 200ms they delay 1.090s - so obviously I am still running from the untuned RC oscillator at measured 7.34MHz....but why? It should not be enabled or selected based on my fuses and setup_oscillator command. What am I doing wrong?

BTW: PCD V4.062


Thanks,
_________________
-Matthew A. Aurand
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Wed Feb 06, 2008 9:37 pm     Reply with quote

Dear Matthew Aurand,

v4.066 is out , solved a lot of problems for my fuses for the osc.

Try it!

Laurent
Storic



Joined: 03 Dec 2005
Posts: 182
Location: Australia SA

View user's profile Send private message Send e-mail

PostPosted: Thu Feb 07, 2008 5:06 am     Reply with quote

Hi,
just a note:
I will be receiving within the week samples of the 24FJ256GB106 MICRO which is similar micro to the 25FJ64GA004 except for
64 pin with 256 K flash 4 x coms 3 x SPI and 3x I2C and 1 x USB (GB)

I guess what I am saying, I will have the micro before CCS has the necessary file suited. I will email a note to CCS tech support on this.
_________________
What has been learnt if you make the same mistake? Wink
Matthew Aurand



Joined: 20 Dec 2007
Posts: 9

View user's profile Send private message

PostPosted: Thu Feb 07, 2008 3:20 pm     Reply with quote

No luck with V4.066, any other ideas?
_________________
-Matthew A. Aurand
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Thu Feb 07, 2008 3:26 pm     Reply with quote

did you try the FUSE HS instead of EC?

Try #FUSES HS and remove #FUSES OSCIO.


Best regards

Laurent


Here's my sample code to have the maximum freq of a DsPIC30F6015 .. datasheet say NEVER EXCEED 120mhz so , a pll of 16x with your HS/2 fuse (HS2_PLL16) will give (40mhz/2)*16 = 320 mhz !!!! impossible to run

it's not a 33F but still ... it work under PCD compiler....

Code:


#include <30f6015.h>
#FUSES FRC_PLL16 // multiply the internal clock of 7.37mhz by 16x
#FUSES PR_PLL // pll mode
#FUSES NOWDT                                       
#FUSES NOPUT                   
#FUSES NOMCLR   

#use delay(clock=117920000) //117.92mhz



or if you want to have the maximum freq. for your ext .40mhz osc.

Code:

#include <30f6015.h>
#FUSES HS2_PLL8 // div by 3 and  multiply the ext. clock of 40mhz by 8x
#FUSES PR_PLL // pll mode
#FUSES NOWDT                                       
#FUSES NOPUT                   
#FUSES NOMCLR   

#use delay(clock=106666667) //106.6666667mhz


Take only my example for reference

Best Regards,

Laurent
Kova



Joined: 06 May 2007
Posts: 35

View user's profile Send private message

PostPosted: Fri Feb 08, 2008 6:38 am     Reply with quote

Hi all,
I have some problems with 24FJ128GA010 PIC and the structs :(

This is a piece of my code:
Code:

#include <24FJ128GA010.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
//#FUSES NOCOE                    //Device will reset into operational mode
//#FUSES ICS1                     //ICD communication channel 1
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES FRC_PLL                  //Internal Fast RC oscillator with PLL
#FUSES NOSKSFSM                 //Clock Switching Mode is disabled
#FUSES NOOSCIO                  //OSC2 is general purpose output
#FUSES NOPR                     //Pimary oscillaotr disabled
#use delay(clock=8000000)

struct codeElement {
  long on; 
  long off;
} const Table[] = {
  { 55,   0 },
  { 273,    92 },
  { 46,    92 },
  {  46,    46 },
  {  46,    46 },
  { 138,   135 },
  {  46,    46 },
  {  46,    46 },
  {  46,    46 },
  {  46,    46 },
  {  46,    46 }
.....//big Table
};

void main{
.....
int i =0;
while (Table[i].on != 0) //Here crash!

cTest = (Table[i].on);
.....
}


I have tested with MPLAB SIM and it crashs :(
I have tested with Proteus and I receive "Illegal data memory access" error :(

P.S.
I'm using 4.065 version and the same code works perfectly on a PIC18F2550 Rolling Eyes

Sorry for my english :(
Thanks

Bye
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Fri Feb 08, 2008 8:46 am     Reply with quote

Code:
#FUSES FRC_PLL 


u need this with this fuse:

Code:
#FUSES PR_PLL // pll mode


Which translate to: Primary OSC. enabled with PLL mode


#FUSES NOPR is to disable your main clock which is kinda contradictory!!


But since you want 8mhz of the internal osc. you don't need the PLL mode

In summary:

1.Remove NOPR fuse
2.Replace PR_PLL by PR
3.Replace FRC_PLL by FRC

There might be some problem with the sim , i suggest to test it on a real pic. The CCS C compiler PCD is still very beta.
Maybe the PCD just compiling code that makes microchip simulator crash.
Try compiling similar code on the Microchip C32 compiler too see if its work.


On some device you might encounter missing pwm , missing timer.. ex: my previously device ,,, dspic30f6015 support at least 5 timers and in PCD i get an error using timer2 or any timer other than timer1, because many devices are incomplete. No pwm support when the chip is known to handle 8 power pwms. Did i mention that i added my fuses manually to the devices .h because i could not get the internal or any HS fuses to work correctly ? Gosh, for me too many trouble for the moment. I will give a shoot later maybe somewhere in the version 4.1 or later

Anyway good luck!

Laurent
Kova



Joined: 06 May 2007
Posts: 35

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 12:30 am     Reply with quote

Thanks ELCouz for the answer Razz
I have tried your your code but no success :(
I will test with a real PIC Smile

Thanks a lot bye
Kova



Joined: 06 May 2007
Posts: 35

View user's profile Send private message

PostPosted: Tue Feb 12, 2008 1:18 am     Reply with quote

I have tried also with a DSPIC30F4013 and these are the errors:

Code:
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x000100
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x000102
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x000104
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x000106
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x000108
..........................................


Confused Confused
Too bugs! Crying or Very sad
markhuang
Guest







dspicDEM 2 board with dspic30f4013 code for PCD
PostPosted: Wed Feb 20, 2008 6:19 am     Reply with quote

Hi Everybody
I am new to this forum but I have been using PIC for 10 years . Most of time I use PIC18f6722 /4525 / 252 . now I move to 30 series.


I HAVE MANAGED TO PORT DSPICDEM 2 BOARD CODE FROM C30 ->PCD
version 4.068 seems to be OK.

the c30 code can be downloaded from microchip website.

my code is as follows :

// TEST ON DSPIC DEM 2 BOARD FEB-2008

#include <30F4013.h>
#include "def_30F4013.h"

#case
#FUSES WDT // Watch Dog Timer
#FUSES XTL
#FUSES PR //Promiary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES WPSB16 //Watch Dog Timer PreScalar B 1:16
#FUSES WPSA512 //Watch Dog Timer PreScalar A 1:512
#FUSES PUT64 //Power On Reset Timer value 64ms
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES LPOL_HIGH //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6)
//PWM module low side output pins have active high output polar
#FUSES HPOL_HIGH //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7)
//PWM module high side output pins have active high output polarity
#FUSES NOPWMPIN //PWM outputs drive active state upon Reset
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOCOE //Device will reset into operational mode
#FUSES ICS0 //ICD communication channel 0
#FUSES RESERVED //Used to set the reserved FUSE bits



#define XTAL_FREQUENCY 7372800
//#use delay(clock=7372800,RESTART_WDT)
#use delay(clock=XTAL_FREQUENCY)

#include "UARTs.c"




#define MAKE_unsigned int16(h,l)(((unsigned int16)h)<< 8)| (l)
#include <string.h>
//used for timer
#include "SPI_for_LCD.c"

#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
int32 Ticker;
int8 Seconds=0 , mySeconds=0;

//optional:
int8 Year=0,Month=0,Days=0,Hours=0,Minutes=0;

////////////////////////////////////////////////////////////////////////////////
// Test whether a given year is a leap year.
// This optimized version only works for the period 2001 - 2099
////////////////////////////////////////////////////////////////////////////////
#define IS_LEAP(year) (year%4 == 0)


////////////////////////////////////////////////////////////////////////////////
// Initialize RTC
////////////////////////////////////////////////////////////////////////////////
void Initialize_RTC(void)
{
///////////////////////////////////////////////////////////
// Zero Drift Real Time Clock
// Original code supplied by Neutone.
// Some small optimizations by C.Kielstra.
///////////////////////////////////////////////////////////


Ticker = TIMER1_FREQUENCY; // initialize clock counter to number of clocks per second
setup_timer1( TMR_INTERNAL | TMR_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
PR1=65535; // must be 65535 .
TMR1=0;
t1if=0;
enable_interrupts( INT_TIMER1 ); // Start RTC
}

void update_timer1_settings(void)
{
setup_timer1( TMR_INTERNAL | TMR_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
PR1=65535; // must be 65535 .
t1ie=1;

}

////////////////////////////////////////////////////////////////////////////////
// -=Process Zero Drift Real Time Clock Information=-
//
// Most algorithms configure the timer to generate an interrupt every 100ms, and
// then count the number of interrupts. The problem in that approach is that most
// clock frequencies can't be divided by 256 and you don't get an exact 100ms.
// The small errors will add up to an error of several seconds a day.
//
// The algorithm presented here is exact in the long run because it doesn't
// count the number of interrupts but counts the number of clock cycles.
////////////////////////////////////////////////////////////////////////////////

/* converted from c30
//_ADCInterrupt() is the A/D interrupt service routine (ISR).
//The routine must have global scope in order to be an ISR.
//The ISR name is chosen from the device linker script.
void __attribute__((__interrupt__)) _ADCInterrupt(void)
{
//Copy the A/D conversion results from ADCBUFn to variables-
//"Potentiometer" and "TempSensor".
//Since ADCON2bits.SMPI = 1, only the first two (i.e. SMPI+1) ADCBUFn
//locations are used by the module to store conversion results
Potentiometer = ADCBUF0;
TempSensor = ADCBUF1;

//Clear the A/D Interrupt flag bit or else the CPU will
//keep vectoring back to the ISR
IFS0bits.ADIF = 0;

}


*/
int16 TempSensor = 0;
int16 Potentiometer = 0;
int1 AD_RDY=0;
#int_ADC1
//_ADCInterrupt() is the A/D interrupt service routine (ISR).
//The routine must have global scope in order to be an ISR.
//The ISR name is chosen from the device linker script.
void ADC_isr(void)
{
//Copy the A/D conversion results from ADCBUFn to variables-
//"Potentiometer" and "TempSensor".
//Since ADCON2bits.SMPI = 1, only the first two (i.e. SMPI+1) ADCBUFn
//locations are used by the module to store conversion results
Potentiometer = ADCBUF0;
TempSensor = ADCBUF1;

//Clear the A/D Interrupt flag bit or else the CPU will
//keep vectoring back to the ISR
AD_RDY=1;
adif = 0;

}



#int_TIMER1
void TIMER1_isr()
{
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker <65536>=b) return(a-b);
else return(b-a);

}
//////////////////////////////////
void bget_string(char* s, int8 max , int8 channel);
//////////////////////////////////

/////////////


////////////
unsigned char adones=0;
unsigned char adtenths=0;
unsigned char adhundredths=0;
unsigned char DisplayData[]="dsPICDEM2 Board\r\nT=+00C Pot=0.00v\r\n\r\n\0";


#include "A_to_D_Converter.c"

/////////////////////////////
void main()
{
char ch , buf1, buf2, buf3,buf4 ,buf5 , temp;
int8 mys;
int16 t1=0 ;
int1 first_use=0;

// setup_spi(SPI_SS_DISABLED);

//// same function
TRISB=0xffff; // all inputs regardless
ADPCFG=0xffff; // set all portbs digital first
//pcfg11=pcfg10=pcfg9=pcfg8=pcfg7=pcfg6=pcfg5=pcfg4=pcfg3=pcfg2=pcfg1=pcfg01=1;


setup_adc_ports(sAN2|sAN3|VSS_VDD);
setup_wdt(WDT_ON);

trisd9=0;
trisb1=0; // output led
trisb0=0; // output led


#define useLATB

#ifndef useLATB
#define LED3 portb0
#define LED4 portb1
#else
#define LED3 latb0
#define LED4 latb1
#endif


IEC0=IEC1=0; // clear UARTS INTERRUPTS FIRST




Initialize_RTC();
enable_interrupts(INT_TIMER1); // t1ie=1;
init_UARTs();

ADC_Init();
SPI_Init();
enable_interrupts(INTR_NORMAL);

Delay5ms(100);

fputs("COMM1",COMM1);
fputs("COMM2",COMM2);




while(1)

{



restart_wdt();
if(mySeconds>0)
{
mySeconds=0;
mys++;

latd9=!latd9;
delay_cycles(1);
LED3=!LED3;
delay_cycles(1);
LED4=!LED4;
if(mys%2==0){
// fprintf(COMM2,"PR1=%u\r\n", PR1);
// fputs(DisplayData , COMM2);
update_timer1_settings();
}
}

if(buffer_remained_UART1) fputc(bgetc(1),COMM1);
if(buffer_remained_UART2) fputc(bgetc(2),COMM2);


if(AD_RDY)
{

UpdateDisplayBuffer();

//if(DisplayData[21]!=buf1 || DisplayData[22]!=buf2 || DisplayData[29]!=buf3
// || DisplayData[31]!=buf4 || DisplayData[32]!=buf5 )
if( DisplayData[29]!=buf3
|| DisplayData[31]!=buf4 || DisplayData[32]!=buf5 )
{


if( DisplayData[29]==buf3 && DisplayData[31]==buf4 )
{
temp=absminus(DisplayData[32],buf5);
// fputc(temp+0x30 , COMM2);
// fputc(':' , COMM2);
if(temp>1 ) {
fputs(DisplayData , COMM2);
WriteSPI_to_LCD();
}
goto fast;
}
fputs(DisplayData , COMM2);
WriteSPI_to_LCD();
}

fast:;
buf1=DisplayData[21];
buf2=DisplayData[22];
buf3=DisplayData[29];
buf4=DisplayData[31];
buf5=DisplayData[32];

AD_RDY=0;

}

if(ferr1)
{

}

if(ferr2)
{

}



if(oerr1){
oerr1=0;
ch=U1RXREG;
}
if(oerr2)
{
oerr2=0;
ch=U2RXREG;
}

}

// TODO: USER CODE!!

}

//////////////////////////////////



////////////*******************************
unsigned char bgetc(int8 channel){
/*
beware that this function will only be triggered by
<or>. so nomally it will not be affected if
typed msg is any unsigned char rather than <or> .
read ring buffer at luxury time no hurry . */


unsigned char a=10,c;
unsigned int16 t=0 ;

switch (channel)
{
case 1 :
while(!buffer_remained_UART1 && a!=0) {

restart_wdt();
if(t++>3000){a--;t=0;}

//stop here to wait incoming char
//also have a timeout about 2 seconds
}

//really time out
if (a==0){
return('>');
}

c=buff_UART1[next_out_UART1];
next_out_UART1=(next_out_UART1+1)% BUFFER_SIZE;
return (c);

break;
case 2 :
while(!buffer_remained_UART2 && a!=0) {




restart_wdt();
if(t++>3000){a--;t=0;}

//stop here to wait incoming char
//also have a timeout about 2 seconds
}

//really time out
//really time out
if (a==0){
return('>');
}
c=buff_UART2[next_out_UART2];
next_out_UART2=(next_out_UART2+1)% BUFFER_SIZE;
return (c);
break ;
default :

break;
}

return(0xff);

}


//////////////////////////////////////////////////

void bget_string(char* s, int8 max, int8 channel) {
int len;
char c;
#define BACK_SPACE_KEY 8
--max;
len=0;
do {

c=bgetc(channel);
if(c==BACK_SPACE_KEY) { // Backspace
if(len>0) {
len--;
if(channel==1)
{ fputc(c, COMM1);
fputc(' ',COMM1);
fputc(c,COMM1);}
else if (channel==2)
{ fputc(c, COMM2);
fputc(' ',COMM2);
fputc(c,COMM2); }


}
} else if ((c>=' ')&&(c<='~'))
if(len<max) {
s[len++]=c;

if(channel==1) fputc(c, COMM1);
else if (channel==2) fputc(c, COMM2);

}
} while(c!=13);
s[len]=0;
}



/////////////////////////////////////////////////////////////////////////////
///////////////////////// special registers for 4013 mcu //////////////
// name def_30f4013.h
#ifndef word
#define word int16
#endif

#word TRISA = 0x02c0
#word PORTA = 0x02c2
#word LATA = 0x02c4

#word TRISB = 0x02c6
#word PORTB = 0x02c8
#word LATB = 0x02ca

#word TRISC = 0x02cc
#word PORTC = 0x02ce
#word LATC = 0x02d0

#word TRISD = 0x02d2
#word PORTD = 0x02d4
#word LATD = 0x02d6

#word TRISF = 0x02de
#word PORTF = 0x02e0
#word LATF = 0x02e2


#bit trisa11=TRISA.11


#bit trisb0=TRISB.0
#bit trisb1=TRISB.1
#bit trisb2=TRISB.2
#bit trisb3=TRISB.3
#bit trisb4=TRISB.4
#bit trisb5=TRISB.5
#bit trisb6=TRISB.6
#bit trisb7=TRISB.7
#bit trisb8=TRISB.8
#bit trisb9=TRISB.9
#bit trisb10=TRISB.10
#bit trisb11=TRISB.11
#bit trisb12=TRISB.12




#bit trisc13=TRISC.13
#bit trisc14=TRISC.14
#bit trisc15=TRISC.15



#bit trisd0=TRISD.0
#bit trisd1=TRISD.1
#bit trisd2=TRISD.2
#bit trisd3=TRISD.3
#bit trisd4=TRISD.4
#bit trisd5=TRISD.5
#bit trisd6=TRISD.6
#bit trisd7=TRISD.7
#bit trisd8=TRISD.8
#bit trisd9=TRISD.9
#bit trisd10=TRISD.10
#bit trisd11=TRISD.11
#bit trisd12=TRISD.12
#bit trisd13=TRISD.13
#bit trisd14=TRISD.14
#bit trisd15=TRISD.15


#bit trisf0=TRISF.0
#bit trisf1=TRISF.1
#bit trisf2=TRISF.2
#bit trisf3=TRISF.3
#bit trisf4=TRISF.4
#bit trisf5=TRISF.5
#bit trisf6=TRISF.6


//////////// port


#bit porta11=PORTA.11


#bit portb0=PORTB.0
#bit portb1=PORTB.1
#bit portb2=PORTB.2
#bit portb3=PORTB.3
#bit portb4=PORTB.4
#bit portb5=PORTB.5
#bit portb6=PORTB.6
#bit portb7=PORTB.7
#bit portb8=PORTB.8
#bit portb9=PORTB.9
#bit portb10=PORTB.10
#bit portb11=PORTB.11
#bit portb12=PORTB.12




#bit portc13=PORTC.13
#bit portc14=PORTC.14
#bit portc15=PORTC.15



#bit portd0=PORTD.0
#bit portd1=PORTD.1
#bit portd2=PORTD.2
#bit portd3=PORTD.3
#bit portd8=PORTD.8
#bit portd9=PORTD.9


#bit portf0=PORTF.0
#bit portf1=PORTF.1
#bit portf2=PORTF.2
#bit portf3=PORTF.3
#bit portf4=PORTF.4
#bit portf5=PORTF.5
#bit portf6=PORTF.6

/// LAT

#bit lata11=LATA.11


#bit latb0=LATB.0
#bit latb1=LATB.1
#bit latb2=LATB.2
#bit latb3=LATB.3
#bit latb4=LATB.4
#bit latb5=LATB.5
#bit latb6=LATB.6
#bit latb7=LATB.7
#bit latb8=LATB.8
#bit latb9=LATB.9
#bit latb10=LATB.10
#bit latb11=LATB.11
#bit latb12=LATB.12




#bit latc13=LATC.13
#bit latc14=LATC.14
#bit latc15=LATC.15



#bit latd0=LATD.0
#bit latd1=LATD.1
#bit latd2=LATD.2
#bit latd3=LATD.3
#bit latd8=LATD.8
#bit latd9=LATD.9


#bit latf0=LATF.0
#bit latf1=LATF.1
#bit latf2=LATF.2
#bit latf3=LATF.3
#bit latf4=LATF.4
#bit latf5=LATF.5
#bit latf6=LATF.6


// input change notification
#word CNEN1 = 0x00c0
#word CNEN2 = 0x00c2
#word CNPU1 = 0x00c4
#word CNPU2 = 0x00c6

#bit cn0ie=CNEN1.0
#bit cn1ie=CNEN1.1
#bit cn2ie=CNEN1.2
#bit cn3ie=CNEN1.3
#bit cn4ie=CNEN1.4
#bit cn5ie=CNEN1.5
#bit cn6ie=CNEN1.6
#bit cn7ie=CNEN1.7
#bit cn8ie=CNEN1.8
#bit cn9ie=CNEN1.9
#bit cn10ie=CNEN1.10
#bit cn11ie=CNEN1.11
#bit cn12ie=CNEN1.12
#bit cn13ie=CNEN1.13
#bit cn14ie=CNEN1.14
#bit cn15ie=CNEN1.15

#bit cn16ie=CNEN2.0
#bit cn17ie=CNEN2.1
#bit cn18ie=CNEN2.2
#bit cn19ie=CNEN2.3
#bit cn20ie=CNEN2.4
#bit cn21ie=CNEN2.5
#bit cn22ie=CNEN2.6
#bit cn23ie=CNEN2.7
#bit cn24ie=CNEN2.8
#bit cn25ie=CNEN2.9
#bit cn26ie=CNEN2.10
#bit cn27ie=CNEN2.11
#bit cn28ie=CNEN2.12
#bit cn29ie=CNEN2.13
#bit cn30ie=CNEN2.14
#bit cn31ie=CNEN2.15
///
#bit cn0pue=CNPU1.0
#bit cn1pue=CNPU1.1
#bit cn2pue=CNPU1.2
#bit cn3pue=CNPU1.3
#bit cn4pue=CNPU1.4
#bit cn5pue=CNPU1.5
#bit cn6pue=CNPU1.6
#bit cn7pue=CNPU1.7
#bit cn8pue=CNPU1.8
#bit cn9pue=CNPU1.9
#bit cn10pue=CNPU1.10
#bit cn11pue=CNPU1.11
#bit cn12pue=CNPU1.12
#bit cn13pue=CNPU1.13
#bit cn14pue=CNPU1.14
#bit cn15pue=CNPU1.15

#bit cn16pue=CNPU2.0
#bit cn17pue=CNPU2.1
#bit cn18pue=CNPU2.2
#bit cn19pue=CNPU2.3
#bit cn20pue=CNPU2.4
#bit cn21pue=CNPU2.5
#bit cn22pue=CNPU2.6
#bit cn23pue=CNPU2.7
#bit cn24pue=CNPU2.8
#bit cn25pue=CNPU2.9
#bit cn26pue=CNPU2.10
#bit cn27pue=CNPU2.11
#bit cn28pue=CNPU2.12
#bit cn29pue=CNPU2.13
#bit cn30pue=CNPU2.14
#bit cn31pue=CNPU2.15

////////// INTERRUP ///////////
#word INTCON1 =0x0080
#bit oscfail = INTCON1.1
#bit skterr = INTCON1.2
#bit addrerr = INTCON1.3
#bit matherr = INTCON1.4
#bit covte = INTCON1.8
#bit ovbte = INTCON1.9
#bit ovate = INTCON1.10
#bit nstdis = INTCON1.15


#word INTCON2 =0x0082
#bit int0ep = INTCON2.0
#bit int1ep = INTCON2.1
#bit int2ep = INTCON2.2
#bit altivt = INTCON2.15

#word IFS0 =0x0084
#bit int0if = IFS0.0
#bit ic1if = IFS0.1
#bit oc1if = IFS0.2
#bit t1if = IFS0.3
#bit ic2if = IFS0.4
#bit oc2if = IFS0.5
#bit t2if = IFS0.6
#bit t3if = IFS0.7
#bit spi1if = IFS0.8
#bit u1rxif = IFS0.9
#bit u1txif = IFS0.10
#bit adif = IFS0.11
#bit nvmif = IFS0.12
#bit si2cif = IFS0.13
#bit mi2cif = IFS0.14
#bit cnif = IFS0.15

#word IFS1 =0x0086
#bit int1if = IFS1.0
#bit ic7if = IFS1.1
#bit ic8if = IFS1.2
#bit oc3if = IFS1.3
#bit oc4if = IFS1.4
#bit t4if = IFS1.5
#bit t5if = IFS1.6
#bit int2if = IFS1.7
#bit u2rxif = IFS1.8
#bit u2txif = IFS1.9
#bit c1if = IFS1.11

#word IFS2 =0x0088
#bit dciif = IFS2.9
#bit lvdif = IFS2.10



#word IEC0=0x008c
#bit int0ie = IEC0.0
#bit ic1ie = IEC0.1
#bit oc1ie = IEC0.2
#bit t1ie = IEC0.3
#bit ic2ie = IEC0.4
#bit oc2ie = IEC0.5
#bit t2ie = IEC0.6
#bit t3ie = IEC0.7
#bit spi1ie = IEC0.8
#bit u1rxie = IEC0.9
#bit u1txie = IEC0.10
#bit adie = IEC0.11
#bit nvmie = IEC0.12
#bit si2cie = IEC0.13
#bit mi2cie = IEC0.14
#bit cnie = IEC0.15


#word IEC1=0x008e
#bit int1ie = IEC1.0
#bit ic7ie = IEC1.1
#bit ic8ie = IEC1.2
#bit oc3ie = IEC1.3
#bit oc4ie = IEC1.4
#bit t4ie = IEC1.5
#bit t5ie = IEC1.6
#bit int2ie = IEC1.7
#bit u2rxie = IEC1.8
#bit u2txie = IEC1.9
#bit c1ie = IEC1.11

#word IEC2=0x0090
#bit dciie = IEC2.9
#bit lvdie =IEC2.10


#word ADPCFG =0x02a8
#bit pcfg11=ADPCFG.11
#bit pcfg10=ADPCFG.10
#bit pcfg9=ADPCFG.9
#bit pcfg8=ADPCFG.8
#bit pcfg7=ADPCFG.7
#bit pcfg6=ADPCFG.6
#bit pcfg5=ADPCFG.5
#bit pcfg4=ADPCFG.4
#bit pcfg3=ADPCFG.3
#bit pcfg2=ADPCFG.2
#bit pcfg1=ADPCFG.1
#bit pcfg01=ADPCFG.0

#word ADCON1=0x02a0
#word ADCON2=0x02a2
#word ADCON3=0x02a4
#word ADCHS=0x02a6
#word ADCSSL=0x02aa

#word ADCBUF0=0x0280
#word ADCBUF1=0x0282 /* ADC Data Buffer 1 0000 uuuu uuuu uuuu */
#word ADCBUF2 =0x0284 /* ADC Data Buffer 2 0000 uuuu uuuu uuuu */
#word ADCBUF3 =0x0286 /* ADC Data Buffer 3 0000 uuuu uuuu uuuu */
#word ADCBUF4 =0x0288 /* ADC Data Buffer 4 0000 uuuu uuuu uuuu */
#word ADCBUF5 =0x028A /* ADC Data Buffer 5 0000 uuuu uuuu uuuu */
#word ADCBUF6=0x028C /* ADC Data Buffer 6 0000 uuuu uuuu uuuu */
#word ADCBUF7=0x028E /* ADC Data Buffer 7 0000 uuuu uuuu uuuu */
#word ADCBUF8=0x0290 /* ADC Data Buffer 8 0000 uuuu uuuu uuuu */
#word ADCBUF9=0x0292 /* ADC Data Buffer 9 0000 uuuu uuuu uuuu */
#word ADCBUFA=0x0294 /* ADC Data Buffer 10 0000 uuuu uuuu uuuu */
#word ADCBUFB=0x0296 /* ADC Data Buffer 11 0000 uuuu uuuu uuuu */
#word ADCBUFC=0x0298 /* ADC Data Buffer 12 0000 uuuu uuuu uuuu */
#word ADCBUFD=0x029A /* ADC Data Buffer 13 0000 uuuu uuuu uuuu */
#word ADCBUFE=0x029C /* ADC Data Buffer 14 0000 uuuu uuuu uuuu */
#word ADCBUFF=0x029E

#word U1RXREG = 0x0212
#bit u1rx8 =U1RXREG.8

#word U2RXREG = 0x021c
#bit u2rx8 =U2RXREG.8

#word U1TXREG = 0x0210
#bit u1tx8 =U1TXREG.8

#word U2TXREG = 0x021a
#bit u2tx8 =U2TXREG.8

#word U1STA=0x020e
#bit oerr1 =U1STA.1
#bit ferr1 =U1STA.1

#word U2STA=0x0218
#bit oerr2 =U2STA.1
#bit ferr2 =U2STA.1

// TIMER1 ///////////
#word PR1=0x0102
#word TMR1=0x0100
#word T1CON=0x0104

//////////SPI //////////////////
#word SPI1STAT=0x0220
#word SPI1CON =0x0222
#word SPI1BUF=0x0224

//////////////////////////////////////////////////////////////////////////
// name UARTs.c


// UART1 is shared with SPI . relocate it now ..

#use rs232(UART1a,baud=9600,parity=N,bits=8,STREAM=COMM1)
#use rs232(UART2,baud=9600,parity=N,bits=8,STREAM=COMM2)


//U1MODE 020C UARTEN — USIDL — — ALTIO — — WAKE LPBACK ABAUD — — PDSEL1 PDSEL0 STSEL
// 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
#word U1MODE=0X020C
#bit stsel=U1MODE.0
#bit pdsel0=U1MODE.1
#bit pdsel1=U1MODE.2
#bit _3=U1MODE.3
#bit _4=U1MODE.4
#bit abaud=U1MODE.5
#bit lpback=U1MODE.6
#bit wake=U1MODE.7
#bit altio=U1MODE.10
#bit usidl=U1MODE.13
#bit uarten=U1MODE.15

#word U2MODE=0X0216
#bit stsel_u2=U2MODE.0
#bit pdsel0_u2=U2MODE.1
#bit pdsel1_u2=U2MODE.2
#bit _3u2=U2MODE.3
#bit _4u2=U2MODE.4
#bit abaud_u2=U2MODE.5
#bit lpback_u2=U2MODE.6
#bit wake_u2=U2MODE.7
#bit altio_u2=U2MODE.10
#bit usidl_u2=U2MODE.13
#bit uarten_u2=U2MODE.15


void init_UARTs(void);
void init_UARTs(void)
{


u1rxie =1; //enable_interrupts(INT_RDA); //
u2rxie =1; // enable_interrupts(INT_RDA2);

}

////////////////////////////////////////////////////////////////
/*
; © 2005 Microchip Technology Inc.
;
; Microchip Technology Inc. (“Microchip”) licenses this software to you
; solely for use with Microchip dsPIC® digital signal controller
; products. The software is owned by Microchip and is protected under
; applicable copyright laws. All rights reserved.
;
; SOFTWARE IS PROVIDED “AS IS.” MICROCHIP EXPRESSLY DISCLAIMS ANY
; WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
; PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP
; BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL
; DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT, COST OF
; PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
; BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
; ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS.
;
;
;FILE DESCRIPTION:
; This file provides low-leve SPI drivers to interface to the 30F2011
; that controls the text-based 4-bit parallel LCD on the dsPICDEM2
; development board.
;
;REVISION HISTORY:
; $Log: SPI_for_LCD.c,v $
; Revision 1.1.1.1 2005/06/06 09:16:46 VasukiH
; First release of software
;
;
*/


// modified by ME to suit CCSC PCD24 compiler .
//Pre-Processor Directives:



#define LCDLINE1CMD 0x80 //Command to set LCD display to Line 1
#define LCDLINE2CMD 0xC0 //Command to set LCD display to Line 2

//Declaration to Link External Functions & Variables:
void Delay5ms (int8 n)
{
int8 i;
do{
delay_ms(5);
} while(i++<n);

}

void Delay5us (int8 n)
{
int8 i;

do{
delay_us(5);
} while(i++<n);
}

extern unsigned char DisplayData[];

//Functions and Variables with Global Scope:
void SPI_Init(void);
void WriteSPI_to_LCD(void);

void tx_hardware_spi(char ch);

/*
struct _IFS0
{
int1 INT0IF :1 ; // bit0
int1 IC1IF :1 ; // bit 1
int1 OC1IF :1 ; // bit2
int1 T1IF :1 ; // bit3
int1 IC2IF :1 ; // bit4
int1 OC2IF :1 ; //5
int1 T2IF :1 ; //6
int1 T3IF :1 ; //7
int1 SPI1IF :1 ; //8
int1 U1RXIF :1 ; //9
int1 U1TXIF :1 ; //10
int1 ADIF :1 ; //11
int1 NVMIF :1; //12
int1 SI2CIF :1 ; //13
int1 MI2CIF :1 ; //14
int1 CNIF :1 ;

} IFS0bits;
*/
/*
struct _IEC0 {
int1 INT0IE :1 ;
int1 IC1IE :1 ;
int1 OC1IE :1 ;
int1 T1IE :1 ;
int1 IC2IE :1 ;
int1 OC2IE :1 ;
int1 T2IE :1 ;
int1 T3IE :1 ;
int1 SPI1IE :1 ;
int1 U1RXIE :1 ;
int1 U1TXIE :1 ;
int1 ADIE :1 ;
int1 NVMIE :1 ;
int1 SI2CIE :1 ;
int1 MI2CIE :1 ;
int1 CNIE :1 ; //15

} IEC0bits ;

struct _SPI1STAT {


//SPI1STAT 0220 SPIEN — SPISIDL — — — — — — SPIROV — — — — SPITBF SPIRBF
// 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

int8 SPIRBF : 2 ; // 0 1
int1 _2 :1 ; // 2
int1 _3 :1 ; // 3
int1 _4 :1 ; // 4
int1 _5 :1 ; //5
int1 SPIROV :1 ; //6
int1 _7 :1 ;
int1 _8 :1 ;
int1 _9 :1 ;
int1 _10 :1 ;
int1 _11 :1 ;
int1 _12 :1 ;
int1 SPISIDL :1 ;
int1 _14 :1 ;
int1 SPIEN :1 ; // 15

} SPI1STATbits ;
*/
#bit spien =SPI1STAT.15
#bit spisidl =SPI1STAT.13
#bit spirov =SPI1STAT.6
#bit spirbf =SPI1STAT.0


unsigned char *LCDCharPtr;

//SPI_Init Function:
//SPI1 module set up to communicate with the LCD controller on-board.
//Note that when SPI1 is enabled on this device, the UART1 pins will not be
//available due to peripheral multiplexing. So this project utilizes
//alternate UART1 pins, U1ATX and U1ARX
void SPI_Init(void)
{



// more reliable !! it is the same !!
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_1);
return;

SPI1STAT = 0x0000;
SPI1CON = 0x0020 ; //Set the SPI1 module to 8-bit Master mode
// IFS0bits.SPI1IF = 0; //Clear the SPI1 Interrupt Flag
spi1if=0;
// IFS0=IFS0bits; // update


// IEC0bits.SPI1IE = 0; //SPI1 ISR processing is not enabled.
//SPI1 will be used in polling-mode
spi1ie=0;
// IEC0=IEC0bits; // update

// SPI1STATbits.SPIEN = 1; //Turn on the SPI1 module
spien=1;
// SPI1STAT=SPI1STATbits; // update




}

//WriteSPI_to_LCD() Function:
//WriteSPI_to_LCD() writes 32 characters to the 2x16 LCD
//using the SPI1 interface in a polling fashion.
//After each byte is written, the Interrupt Flag bit is polled and the
//next character is written after the interrupt flag bit is set.
// name : SPI_for_LCD.c

void WriteSPI_to_LCD(void)
{
int8 temp, i;

unsigned char DisplayData1[]="TEST ME v1.0\r\n\r\n\0";



tx_hardware_spi(LCDLINE1CMD); //First write the command to set cursor
//to Line1 on LCD
LCDCharPtr = &DisplayData[0]; //Set up LCD character pointer to point

//to the Display buffer

while(i < 20)
{
while (spi1if==0); //Now write 16 characters
temp=*LCDCharPtr;
if(temp==10 || temp==13 ) break;
tx_hardware_spi(temp);
LCDCharPtr++;
i++;
}

while (spi1if==0);


do{
temp = *LCDCharPtr++; // skip cr lf etc
} while(temp==10 || temp==13);

tx_hardware_spi(LCDLINE2CMD);
tx_hardware_spi(temp);
//Next, write the command to set cursor
// line2 on LCD
i = 0;
while(i < 20)
{
while (spi1if==0); //Now write 16 characters
temp=*LCDCharPtr;
if(temp==10 || temp==13 ) break;
tx_hardware_spi(temp);
LCDCharPtr++;
i++;
}


}

void tx_hardware_spi(char ch)
{
int8 temp, i;
temp = SPI1BUF; // read dummy buffer . purge it
spi1if=0; // clear flag if
spirov = 0;
Delay5us(50); // found without this delay it is still ok .
SPI1BUF =ch;


}

////////////////////////////////////////////
/////////////////////////
// name A_to_D_converter.c

void ADC_Init(void);
void ADCResult2Decimal(unsigned int16 ADRES);
void UpdateDisplayBuffer(void);
extern unsigned char DisplayData[];

struct _ADCON1
{
int1 DONE :1 ; // 0
int1 SAMP :1 ; // 1
int1 ASAM :1 ; //2

int1 _3 :1 ;
int1 _4 :1 ;
int8 SSRC :3 ;
int1 FORM :2 ;
int1 _10 :1 ; // 10
int1 _11 :1 ; // 10
int1 _12 :1 ; // 10
int1 ADSIDL :1 ;
int1 _14 : 1 ;
int1 ADON :1 ;

} ADCON1bits ;

struct _ADCON2
{
int1 ALTS :1 ; // 0
int1 BUFM :1 ; // 1
int8 SMPI :4 ; //2
int1 _6 :1 ;
int1 BUFS :1 ;
int1 _8 :1 ;
int1 _9 :1 ;
int1 CSCNA :1 ; // 10
int1 _11 :1 ;
int1 _12 :1 ;
int8 VCFG :3 ;

} ADCON2bits ;


struct _ADCON3
{
int8 ADCS :6 ;
int1 _6 :1 ;
int1 ADRC :1 ;
int8 SAMC :5 ;

int1 _13 :1 ;
int1 _14 :1 ;
int1 _15 :1 ;

} ADCON3bits ;

//Functions:

//ADC_Init() is used to configure A/D to scan and convert 2 input channels
//per interrupt. The A/D is set up for a total sampling rate of 8KHz
//or 4KHz per channel. The internal counter in the A/D is used to provide
//Acquisition time delay. The input pins being scanned are AN2 and AN3.
//AN2 and AN3 are connected to the Temperature Sensor and the Potentiometer
//on the dsPICDEM2 board.
void ADC_Init(void)
{



//ADCON1 Register
//Set up A/D for Automatic Sampling, Auto-Convert
//All other bits to their default state

ADCON1bits=ADCON1; // RESTORE OLD VA;LUE
ADCON1bits.SSRC = 7;
ADCON1bits.ASAM = 1;

ADCON1=ADCON1bits; // update
//ADCON2 Register
//Set up A/D for interrupting after 2 samples get filled in the buffer
//Also, enable Channel scanning
//All other bits to their default state

ADCON2bits=ADCON2; // RESTORE OLD VA;LUE
ADCON2bits.SMPI = 1;
ADCON2bits.CSCNA = 1;
ADCON2bits.ALTS=0;
ADCON2=ADCON2bits;
//ADCON3 Register
//Set up Acquisition time (Tacq) for 31 Tad periods
//where, Tad = A/D conversion clock time.
//Set up Tad period to be 20.5 Tcy (Tcy = instruction cycle time)
//Given that each conversion takes 14*Tad (=Tconv) periods,
//Total Sample Time = Acquisition Time + Conversion Time
// = (31 + 14)*Tad = 45*Tad periods
// = 45 * 20.5 * Tcy = 922.5*Tcy periods
//At 7.3728 MIPS, Tcy = 135 ns = Instruction Cycle Time
//So Tsamp = Tacq + Tconv = 45*Tad(in this example)= 125.1 microseconds
//So Fsamp = Sampling Rate ~= 8 KHz
//All other bits to their default state

ADCON3bits=ADCON3; // RESTORE OLD VA;LUE

ADCON3bits.SAMC = 31;
ADCON3bits.ADCS = 40;
ADCON3=ADCON3bits;
//ADCHS Register
//When Channel scanning is enabled (ADCON2bits.CSCNA=1)
//AND Alternate mux sampling is disabled (ADCON2bits.ALTS=0)
//then ADCHS is a "don't care"
ADCHS = 0x0000;

//ADCSSL Register
//Scan channels AN2, AN3 fas part of scanning sequence
ADCSSL = 0x000C;

//ADPCFG Register
//Set up channels AN2, AN3 as analog inputs and leave rest as digital
//Recall that we configured all A/D pins as digital when code execution
//entered main() out of reset

// before the a/d pin can be used . make it input !
trisb2=1; // input
trisb3=1; // input
pcfg2=0; // ADPCFGbits.PCFG2 = 0;
pcfg3=0;// ADPCFGbits.PCFG3 = 0;

//Clear the A/D interrupt flag bit
adif=0; //IFS0bits.ADIF = 0;

//Set the A/D interrupt enable bit
adie=1; // IEC0bits.ADIE = 1;

//Turn on the A/D converter
//This is typically done after configuring other registers

ADCON1bits.ADON = 1; // modify
ADCON1=ADCON1bits; // update now .
}


//////////
void UpdateDisplayBuffer(void)
{
int16 temp = 0; //Convert TC1047A reading to Deg
//Celsius
temp = TempSensor - 0x199; //Determine if the temperature is
if (temp > 0) //negative, in order to write
{ //either a + or - sign to display areas
DisplayData[20] = '+';
}
else
{
temp = 0x199 - TempSensor;
DisplayData[20] = '-';
}

ADCResult2Decimal(temp); //Convert the hex value to decimal
DisplayData[21] = adtenths; //Update the display buffer characters
DisplayData[22] = adhundredths;

ADCResult2Decimal(Potentiometer); //Convert the A/D hex value to
DisplayData[29] = adones; //decimal and update the display buffer
DisplayData[31] = adtenths;
DisplayData[32] = adhundredths;

}


/////////////////////////
//ADCResult2Decimal() Function:
//This function converts a 12-bit A/D result in hexadecimal to decimal
//assuming a 5 volt reference voltage.
void ADCResult2Decimal(unsigned int16 ADRES)
{
adones = 0; //reset values
adtenths = 0;
adhundredths = 0;
while (ADRES > 0x8)
{
if(ADRES > 0x333) //test for 1 volt or greater
{
adones++; //increment 1 volt counter
ADRES -= 0x334; //subtract 1 volt
}
else if(ADRES > 0x51 && ADRES <= 0x333) //test for 0.1 volt or greater
{
if (adtenths <9> 0x8 && ADRES <= 0x51) //test for 0.01 volt or greater
{
if (adhundredths < 9)
{
adhundredths++; //increment hundreths
}
else
{

adhundredths = 0; //reset hundredths
if (adtenths < 9)
{
adtenths++; //and increment tenths
}
else
{
adones++; //unless tenths has rolled over
adtenths = 0; //so increment ones and reset tenths
}
}
ADRES -= 0x9;
}



}
adones += 0x30; // make sure in '1-9' range !
adtenths += 0x30;
adhundredths += 0x30;

}



/////////////////////////////////////////
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Mar 20, 2008 6:13 pm     Reply with quote

How successful is the latest PCD? Anyone having success at using for their projects?
mickent



Joined: 18 Feb 2007
Posts: 22
Location: TN, USA

View user's profile Send private message

PostPosted: Fri Mar 21, 2008 11:43 am     Reply with quote

I have been struggling away with both a PIC24 and a PIC33 as I want the 12 bit A/D.
If I was doing this as a commercial venture I would be in trouble but as a retired machine controls EE I enjoy the mental challenge. Shocked
The open drain feature is very nice for interfacing with 5v devices; Just do a #word ODCx 0xXXXX to put a "1" for open drain.
CCS support has been very responsive to my email questions.
I am doing a drag car data collection system so the large RAM is nice to store six seconds of data before writing out to a SD card.
I emailed Darren to start a separate section for the 16 bit chips but I guess he does not agree? Maybe if he heard from others in "PCD purgatory"?
_________________
Mick
JoaoSantos



Joined: 19 Jun 2007
Posts: 20
Location: Castelo Branco, Portugal

View user's profile Send private message Visit poster's website

PostPosted: Thu Mar 27, 2008 10:35 am     Reply with quote

mickent wrote:
I have been struggling away with both a PIC24 and a PIC33 as I want the 12 bit A/D.
If I was doing this as a commercial venture I would be in trouble but as a retired machine controls EE I enjoy the mental challenge. Shocked
The open drain feature is very nice for interfacing with 5v devices; Just do a #word ODCx 0xXXXX to put a "1" for open drain.
CCS support has been very responsive to my email questions.
I am doing a drag car data collection system so the large RAM is nice to store six seconds of data before writing out to a SD card.
I emailed Darren to start a separate section for the 16 bit chips but I guess he does not agree? Maybe if he heard from others in "PCD purgatory"?


Hello, I think this opinion says it all. I'm on the other side of the barrier, this is, I am developing comercial applications with DSPICS (33F) using the latest version 4.069, and i'm in trouble (to the neck), most of the times due to the (very) buggy compiler Sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group