สวัสดีค่ะ
หนูต่อใช้งาน PIC16F887 กับ MCP3551 สามารถใช้งานได้ แต่พอเปลี่ยนเบอร์เป็น PIC18F452 โดยใช้วงจรเดิมทุกอย่างแล้วใช้ไม่ได้
รบกวนพี่ ๆ ชี้แนะด้วยค่ะ ว่าหนูต้องเขียน code อะไรเพิ่มหรือเปล่า
code มีดังนี้ค่ะ
#include <18F452.h>
#fuses XT, NOWDT, NOLVP
#device *=16
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,)
#include <lcd.c>
#include <stdlib.h>
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#define Vbe 0.00000238418579101562
//5/(2^21)
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
#define LF 10
#define CR 13
int flag;
union combine {
int32 word;
int8 b[4];
};
#byte PORTC=0x7
#bit SDI=PORTC.4 //will need a bit defintion here for CS
#bit CS =PORTC.2
//.........................
int32 read3551(void)
{
union combine temp;
//drop CS
CS=0;
delay_cycles(1);
CS=1;
delay_cycles(1);
//At this point conversion has been triggered.
CS=0;
delay_us(1);
//wait for conversion to complete
while (SDI==1) {
CS=1;
delay_cycles(1);
CS=0;
}
//Now read data.
temp.b[2]=spi_read(0);
temp.b[1]=spi_read(0);
temp.b[0]=spi_read(0);
temp.b[3]=0;
CS=1;
return temp.word;
}
//======================
#INT_EXT
void IntRB0_isr(void)
{
flag = 1;
}
//=======================
void main()
{
int32 value;
float volt;
set_tris_a(0b00000000);
set_tris_b(0b00000011);
set_tris_c(0b10010000); // C3 = CLK out, C4 = SDI=1,
// C2 = CS C6=XMIT C7=RCV=1
set_tris_d(0x1F);
//...............................
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge( H_TO_L );
port_b_pullups(true);
//..........................
CS=1; //Start with CS high
setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_4 );
output_low(pin_A3);
delay_ms(500);
output_high(pin_B5); //sent 5 V to load cell
delay_ms(500);
//...............................
lcd_init();
delay_ms(500);
//..............................
flag = 0;
printf(lcd_putc,"\f Put new sample \n");
delay_ms(3000);
while(TRUE)
{
value = read3551();
volt = Vbe*(float)value;
printf(lcd_putc,"\nVolt= %f ", volt);
delay_ms(1000);
}
}
รบกวนชี้แนะ code ccs c ใน mode SPI
หนูต่อใช้งาน PIC16F887 กับ MCP3551 สามารถใช้งานได้ แต่พอเปลี่ยนเบอร์เป็น PIC18F452 โดยใช้วงจรเดิมทุกอย่างแล้วใช้ไม่ได้
รบกวนพี่ ๆ ชี้แนะด้วยค่ะ ว่าหนูต้องเขียน code อะไรเพิ่มหรือเปล่า
code มีดังนี้ค่ะ
#include <18F452.h>
#fuses XT, NOWDT, NOLVP
#device *=16
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,)
#include <lcd.c>
#include <stdlib.h>
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#define Vbe 0.00000238418579101562
//5/(2^21)
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
#define LF 10
#define CR 13
int flag;
union combine {
int32 word;
int8 b[4];
};
#byte PORTC=0x7
#bit SDI=PORTC.4 //will need a bit defintion here for CS
#bit CS =PORTC.2
//.........................
int32 read3551(void)
{
union combine temp;
//drop CS
CS=0;
delay_cycles(1);
CS=1;
delay_cycles(1);
//At this point conversion has been triggered.
CS=0;
delay_us(1);
//wait for conversion to complete
while (SDI==1) {
CS=1;
delay_cycles(1);
CS=0;
}
//Now read data.
temp.b[2]=spi_read(0);
temp.b[1]=spi_read(0);
temp.b[0]=spi_read(0);
temp.b[3]=0;
CS=1;
return temp.word;
}
//======================
#INT_EXT
void IntRB0_isr(void)
{
flag = 1;
}
//=======================
void main()
{
int32 value;
float volt;
set_tris_a(0b00000000);
set_tris_b(0b00000011);
set_tris_c(0b10010000); // C3 = CLK out, C4 = SDI=1,
// C2 = CS C6=XMIT C7=RCV=1
set_tris_d(0x1F);
//...............................
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge( H_TO_L );
port_b_pullups(true);
//..........................
CS=1; //Start with CS high
setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_4 );
output_low(pin_A3);
delay_ms(500);
output_high(pin_B5); //sent 5 V to load cell
delay_ms(500);
//...............................
lcd_init();
delay_ms(500);
//..............................
flag = 0;
printf(lcd_putc,"\f Put new sample \n");
delay_ms(3000);
while(TRUE)
{
value = read3551();
volt = Vbe*(float)value;
printf(lcd_putc,"\nVolt= %f ", volt);
delay_ms(1000);
}
}