View previous topic :: View next topic |
Author |
Message |
Armer175
Joined: 13 Jan 2009 Posts: 12
|
PIC18F8723 Setup |
Posted: Tue Mar 10, 2009 10:07 am |
|
|
OK, I am starting a new project. I will be using two different SPI device on SPI2 and I2C device(which is on SPI1 PINs). I also will be reading in data from GPS (UART1) and using UART2 as a debug output. I have two analog inputs and then a few digital I/O.
I decide to make sure each device seperately is working so I will know if my problem is in the code or the interface.
I am trying to print out "hello world" through the first uart but it is not working. I hook up the second uart and it is working. When I use a printf statement I do I tell it what UART to use? Here is my code sofar.
.H File:
Code: |
#include <18F8723.h>
//#device ICD=TRUE
#device adc=12
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC //Internal Osc
#FUSES NOPROTECT //Code not protected from reading
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
//#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES MCU //Microcontroller Mode
#FUSES NOWAIT //Wait selections unavailable for Table Reads or Table Writes
#FUSES BW16 //16-bit external bus mode
#FUSES ABW16 //16-bit Address bus
#FUSES ECCPE //Enhanced CCP PWM outpts multiplexed with RE6 thorugh RE3
#use delay(clock=8000000)
//#define I2C_SCL PIN_C3
//#define I2C_SDA PIN_C4
//#define TX1 PIN_C6
//#define RX1 PIN_C7
//#define SPI2_SCK2 PIN_D6
//#define TX2 PIN_G1
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,UART1)
#use rs232(baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8,UART2)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)
|
Main Program:
Code: |
#include <header> //I use the full pathname in actual program
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi2(SPI_MASTER|SPI_MODE_3|SPI_CLK_DIV_16);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
Output_High(PIN_E7);
While(1)
{
Output_Low(PIN_E7);
Spi_Write2(0x20);
Output_High(PIN_E7);
delay_us(1);
printf("Hello World\r\n");
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 10, 2009 10:21 am |
|
|
Do all RS-232 operations with "streams". Use the CCS stream functions
for this. Use fprintf() instead of printf(). Use fputc() instead of putc(),
etc. In each of those functions you specify the stream that is used.
See the manual for more information. |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Tue Mar 10, 2009 10:28 am |
|
|
for ex:
Code: |
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, STREAM = S1, UART1)
#use rs232(baud=9600,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8, STREAM = S2, UART2)
.
.
.
.
.
putc(data, S2); //or fputc
.
.
.
fprintf(S1, "hello world");
.
.
.
|
|
|
|
Armer175
Joined: 13 Jan 2009 Posts: 12
|
|
Posted: Tue Mar 10, 2009 10:33 am |
|
|
Thanks works great now! |
|
|
Armer175
Joined: 13 Jan 2009 Posts: 12
|
|
Posted: Tue Mar 10, 2009 12:02 pm |
|
|
#define SD21 0xC2 // SD21 I2C Address
int16 servo1;
int8 servo1LowByte;
int8 servo1HighByte;
int8 Speed;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi2(SPI_MASTER|SPI_MODE_3|SPI_CLK_DIV_16);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_4MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
Speed = 0;
servo1=1500; // For Parallax servo 2200-500 center at 1400
servo1LowByte = make8(servo1, 0);
servo1HighByte = make8(servo1, 1);
Output_High(PIN_E7);
While(1)
{
//Output_Low(PIN_E7);
//Spi_Write2(0x20);
//Output_High(PIN_E7);
//delay_us(1);
//fprintf(UART1,"Hello World\r\n");
i2c_start();
i2c_write(SD21);
i2c_write(0x00); // send register address
i2c_write(Speed); // send the data
i2c_write(servo1LowByte); // send the data
i2c_write(servo1HighByte); // send the data
i2c_stop();
}
}
Last edited by Armer175 on Tue Mar 10, 2009 12:23 pm; edited 2 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 10, 2009 12:22 pm |
|
|
CCS combines the 7-bit i2c slave address and the R/W bit into one byte.
So in the CCS world, the slave address is shifted to left by one bit.
In the Philips world, an i2c slave address consists of 7 bits, and is not
shifted. It's right justified.
This is entirely an issue of representation, and manner of thinking.
It could also be an issue of interpretation by your logic analyzer's
protocol analyzer. An i2c slave address of 0x61 in Philips format, is
0xC2 in CCS format. |
|
|
Armer175
Joined: 13 Jan 2009 Posts: 12
|
|
Posted: Tue Mar 10, 2009 7:33 pm |
|
|
I did connect some servos to the controller later and it was working correctly. Just goes along with what you were saying about the W/R bit. Thanks again PCM Programmer |
|
|
Armer175
Joined: 13 Jan 2009 Posts: 12
|
New Problem |
Posted: Wed Mar 11, 2009 12:47 pm |
|
|
I am now having some problems reading from the spi device(ADIS16350)
datasheet:
http://www.analog.com/static/imported-files/data_sheets/ADIS16350_16355.pdf
I want to read in the "supply out" out register (pg14). But Iam not getting correct data out. I dont think i am reading the register correctly. PLEASE Help.
Code: |
While(1)
{
Output_Low(PIN_E7);
Spi_Write2(0x03); //send out address to read from
SUPPLY_OUT_HIGH=SPI_READ2(0); //read highbyte
Output_High(PIN_E7);
//DELAY_MS(10);
Output_Low(PIN_E7);
Spi_Write2(0x02);
SUPPLY_OUT_LOW=SPI_READ2(0); //read lowbyte
Output_High(PIN_E7);
SUPPLY_OUT=MAKE16(SUPPLY_OUT_HIGH, SUPPLY_OUT_LOW); //combine highbyte and lowbyte
BIT_CLEAR(SUPPLY_OUT,15&14&13&12); //cleart top bits that are not needed.
voltage=SUPPLY_OUT*1.8315e-3; //calculate voltage
fprintf(UART1,"total %lx voltage %2.5f \r\n",SUPPLY_OUT, voltage);
//servocontrol();
DELAY_MS(1000);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 11, 2009 2:09 pm |
|
|
You need to write a driver for the ADIS16350 chip.
It needs to be written as a separate file, named ADIS16350.c.
It should have functions to read and write to the chip.
You need to try to write the ADIS16350 driver by yourself,
and if you have problems then start a new thread about it.
Here is a thread on another ADIS chip, where I have explained how
to setup the correct SPI mode.
http://www.ccsinfo.com/forum/viewtopic.php?t=38016&start=5
Do not add your questions onto that thread.
Look at this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=38131
Notice my suggestions on how to clean up the code.
Quote: | BIT_CLEAR(SUPPLY_OUT,15&14&13&12); |
This is an invention by you. Never do this. We will spot it immediately.
The CCS manual says you may use a single number, from 0 to 31
for that parameter. It's just wasting our time if you invent things.
Quote: | bit_clear( )
Syntax: bit_clear(var, bit)
Parameters: var may be a any bit variable (any lvalue)
bit is a number 0-31 representing a bit number, 0 is the least significant bit. |
Quote: | SUPPLY_OUT_LOW=SPI_READ2(0); |
Don't use ALL CAPS for functions and variables. It's not the C standard.
Use lower case or mixed case. |
|
|
Armer175
Joined: 13 Jan 2009 Posts: 12
|
|
Posted: Wed Mar 11, 2009 3:22 pm |
|
|
Is there an advantage to using a driver file other than making the main look cleaner? I was going to create seperate functions outside the main.
I haven't programmed in C for a long time so its been rough trying to write nice code. Thanks again for your help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 11, 2009 3:51 pm |
|
|
The driver file allows you to create your own personal library of known-
good, working drivers. You can just "drop" the driver into a new project
(with an #include statement) and you know it works. |
|
|
|