// This program will send 0-99 with a delay of 1s between the data
#BYTE TXSTA=0x98
#BYTE SPBRG=0x99
#BYTE RCSTA=0x18
#BYTE TXREG=0x19
#BYTE TRISB=0x86
#BYTE TRISA=0x85
#BYTE PCON=0x8E
#BYTE PIE1=0x8C
#BYTE PORTA=0x05
#BIT RCIE=PIE1.5
#BIT TXIF=PIE1.4
#BIT RA1=PORTA.0
#BIT RA2=PORTA.1
#BIT TRMT=TXSTA.1
int i=0;
void send_data(int x){
TXREG=x;
while(TRMT==1){};
delay_ms(50);
}
void enable_serial(){
SPBRG=0x19; //0x19 = 9600 Baud - 4MHz
TXSTA=0x24;
RCSTA=0x80;
}
void disable_serial(){
TXSTA=0x00;
RCSTA=0x00;
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
enable_serial();
TRISB=0x00;
bit_set(PCON,3);
// TODO: USER CODE!!
while(1)
{
enable_serial();
for(i=0;i<100;i++)
{
send_data(i);
delay_ms(1000);
}
}
}
thnx a lot manIt can be simpler!
You only have to set up the UART registers once so you can put the "enable_serial()" code at the beginning of main(), thereis no need to put it in a loop. You can also remove the delay in the "send_data()" routine as it does nothing except add to the delay between sending each character in the main program.
Also note that you are only sending numbers with this program, not receiving anything and the numbers will be 0 to 99 in binary, not printable characters.
Brian.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 while(GODONE) { v=read_adc()/205.7; R=v/0.001; t=(R-1)/25974.025; delay_us(1000); ADON=0; }
Code C - [expand] 1 2 3 4 5 6 while(GODONE) //loop until GODONE is true v=read_adc()/205.7; R=v/0.001; t=(R-1)/25974.025; delay_us(1000); ADON=0;
I'm not familiar with that 'dialect' of C but I cannot understand what the config_adc() routine does and why it needs so many delays. I think you will also have problems with the math calculations because you are mixing integers with floating point values.
Doesn't "#include <16F887.h>" tell the program the PICs register and bit addresses? Is it necessary to include them twice?
I'm asuming statements like "#bit GODONE= ADCON0.1" is the same as normal C would write as "#define GODONE ADCON0.1" but again that should already be defined in the header file.
Brian.
# include <16F887.h> allows the compiler to find the location of the project file
Are you using CCS C Compiler? Is read_adc() a library function? If yes, how does it know which adc channel to read? You are using many ADC channels and I think you have to configute ANSELH = )FF for some channels you have used.
The below code is wrong.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 while(GODONE) { v=read_adc()/205.7; R=v/0.001; t=(R-1)/25974.025; delay_us(1000); ADON=0; }
It should be
Code C - [expand] 1 2 3 4 5 6 while(GODONE) //loop until GODONE is true v=read_adc()/205.7; R=v/0.001; t=(R-1)/25974.025; delay_us(1000); ADON=0;
but if read_adc() is a library function then you don't need all those codes. The whole process of reading and returning adc value is done by read_adc() function.
egarding the "while", you must use the { } ,this is a syntax that must be respected
config adc () routine allows me to set the clock to the ADC and to choose the analog inputs to be used
# include <16F887.h> allows the compiler to find the location of the project file
If you are using library functions of CCS C Compiler then you don't have to write all those extra adc codes you have written. Post your latest code. When I have some time I will look into CCS C manual and see what adc library functions you need to use for adc and write a working code.
#include <16F887.h>
#DEVICE ADC=10
#use delay(clock=4000000)
#use rs232(Baud=9600,Xmit=pin_C6,rcv=pin_C7,parity=N,BITS=8)
#byte PORTA= 0x05
#byte PORTB= 0x06
#byte PORTC= 0x07
#byte PORTD= 0x08
#byte TRISA= 0x85
#byte TRISB= 0x86
#byte TRISC= 0x87
#byte TRISD= 0x89
#byte TXSTA= 0X98
#byte RCSTA= 0X18
#BYTE SPBRG= 0x99
#byte ADRESL=0x9E
#BYTE TXREG= 0x19
#byte ADCON1=0x9F
#byte ADCON0=0x1F
#byte RCSTA= 0x18
#byte ANSEL= 0x188
#byte PIE1 = 0x8c
#byte PIR1 = 0x0c
#BYTE SPBRG=0x99
#BYTE TRISB=0x86
#BYTE TRISA=0x85
#BYTE PCON= 0x8E
#BYTE PORTA=0x05
#bit GODONE= ADCON0.1
#bit ADFM = ADCON1.7
#bit VCFG0=ADCON1.4
#bit VCFG1=ADCON1.5
#bit adcs1=ADCON0.7
#bit adcs0=ADCON0.6
#bit ADON=ADCON0.0
#bit ADIE=PIE1.6
#bit ADIF=PIR1.6
///////////////////////
#BIT RCIE=PIE1.5
#BIT TXIF=PIE1.4
#BIT RA1=PORTA.0
#BIT RA2=PORTA.1
#BIT TRMT=TXSTA.1
int i=0;
float v,R,T;
/////////////// Initialisation des ports ///////////////
void pic_init()
{
set_tris_A(0XAF);
set_tris_B(0X3F);
set_tris_C(0X81);
set_tris_D(0X80);
set_tris_E(0X07);
}
/////////////// Configuration du CAN ////////////////
void config_adc()
{
setup_adc(ADC_clock_internal);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
}
//////// lecture et conversion de la température capté par les sondes ///////
void lect_temp()
{
///int R;
ADON=1;
GODONE=1;
ADIF=0;
ADIE=1;
while(GODONE)
{
v=read_adc();
delay_us(1000);
}
ADON=0;
R=v/0.2057; //0.001;
T=(R-1)/25974.025;
}
///////////////////////////// configuration du USART ///////////////////////////
void config_UART( )
{
TXSTA=0X24;
RCSTA=0X90;
}
/////////////////// Configuration du rs232 /////////////////////
void send_data(int x)
{
x=T;
TXREG=x;
while(TRMT==1);
{
delay_ms(50);
}
}
void enable_serial()
{
SPBRG=0x19; //0x19 = 9600 Baud - 4MHz
TXSTA=0x24;
RCSTA=0x80;
}
void disable_serial()
{
TXSTA=0x00;
RCSTA=0x00;
}
////////////////////// programme principal //////////////////////
void main()
{
float temp;
pic_init();
ADRESL=0;
ADFM=1;
ANSEL=0XFF;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
config_adc();
temp=lect_temp();
config_uart();
TXREG=temp;
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
enable_serial();
bit_set(PCON,3);
// TODO: USER CODE!!
enable_serial();
send_data(i);
delay_ms(1000);
}
Is your LCD code working?
It's getting better and shorter as well but please remove all the lines starting with #byte and #bit, they should not be necessary. They are already declared in the ".h" file so you don't need to declare them again.
Also, you probably want to use the value 0x90 instead of 0x80 in RCSTA inside your enable_serial() routine.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?