We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]
posted on 2011/11/17 01:19:45 PM CET
I want to Build a Data logging system using microcontroller PIC18F4520 and Ethernet controller ENC28j60 which would read the analog value from temp sensor LM35 and upload the value from the sensor to https://pachube.com/ to store and graph the data.
posted on 2011/11/11 11:42:43 AM CET
Communication > CAN / LIN / RS485
Hello.I wrote a program using RS485 [ PIC18F452/XTAL=20MHZ/mikroC for PIC compiler v5.1.0 ] to transfer the following arrays;
unsigned long int LOWD[]={1000,1000,1000,1000,1000,1000,500,500,500,100,4,44,40,7,5,5,5,400,500000,300000};
unsigned long int DHIGH[]={25000,25000,25000,25000,25000,25000,1200,1200,1200,290,11,60,280,1,33,33,33,3190,30000000,30000000};
from U2=PIC18F452 as a master to U1=PIC18F452 as a slave just at the begining of my main
and for one time and also to transfer the above arrays from U1=PIC18F452 slave for each
5 seconds to a PC.U2 is about 800 meters far from U1 and PC is 650 meters far from U2 and U2.
So,I wrote the following code for my PIC Master U2;
/*
*** Using RS485***
MCU: PIC18F452.
Oscillator: External, 20.000 MHz.
Ext. Modules: RS485 on PORTC.
SW: mikroC v5.1.
*/
char datx[39],dat[39];//Buffer for receving/sending messages.
char i,k, Iflag;
unsigned long int LOWD[]={1000,1000,1000,1000,1000,1000,500,500,500,100,4,44,40,7,5,5,5,400,500000,300000};
unsigned long int DHIGH[]={25000,25000,25000,25000,25000,25000,1200,1200,1200,290,11,60,280,1,33,33,33,3190,30000000,30000000};
//Function declration;
void MRSX485();
void MRSX485(){
UART1_Init(9600);//initialize uart module.
Delay_ms(100);
RS485Master_Init();//intialize mcu as master.
PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;
for(i=0;i<20;i++){
datx[i]=LOWD[i];
}
for(i=20;i<40;i++){
datx[i]=DHIGH[i];
}
k=0;
MX1:
dat[0] = datx[k];
dat[1] = datx[k+1];
dat[2] = datx[k+2];
dat[3] = 0;
dat[4] = 0; //*****What I should assign to dat[3] thru dat[6]?*****
dat[5] = 0;
dat[6] = 0;
Iflag = 0;
RS485Master_Send(dat,1,160);
PORTB = 0;
PORTD = 0;
dat[4] = 0;//ensure that message received flag is 0.
dat[5] = 0;//ensure that error flag is 0.
PIE1.RCIE = 1;//Enable interrupts.
INTCON.PEIE = 1;//On byte received.
PIE2.TXIE = 0;//Via UART (RS485).
INTCON.GIE = 1;
//**? **upon completed valid message receiving.
//**? **data[4] is set to 255.
if(dat[5]){ //if error detected, signal it by.
PORTD.F0 = 1;//setting PORTD.F0 to 1.
goto MX1;//Repeat sending that error messages again untill to be sent successfully.
}
if(dat[4]){ //if message received successfully.
dat[4] = 0;//clear message received flag.
PORTD.F1=1;//setting PORTD.F1 to 1.
Delay_ms(1);
K=K+3;
goto MX1; //Go for sending the remains.
}
}//~
// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}//~
sbit rs485_rxtx_pin at RC6_bit;//set transcieve pin.
sbit rs485_rxtx_pin_direction at TRISC6_bit;//set transcieve pin directio.
void main() {
PORTA=0;//Clear portA.
TRISA=0XFF;//PortA is input.
PORTD=0;//Clear portD.
TRISD=0;//PortD is output.
PORTC=0;//Clear portC.
TRISC=0;//portC is output.
PORTB=0;//Clear portB.
TRISB=0XFF;//portB is input.
PORTE=0;//Clear portE.
TRISE=0;//PortE is Output. zzzz
TRISC = PORTC = 0;
// Only for PIC18F452 //
RCON.IPEN =1;//Enable priority levels on interrupts.
INTCON.INT0IE =1;//Enables the INT0 external interrupt.
INTCON2.INTEDG0 =0;//Interrupt on falling edge.
INTCON.RBIE =1;//Enables the RB Port change interrupt.
Delay_ms(400);
MRSX485();
}//~!
But that is not worked correctly.Would you please tell me what is wrong with my code?
Also,how can I sure that my arrays transferred correctly into U1 in a effective,fast and simply?
And how to write the Slave[U1] for that,to receive arrays send byU2?
Thanks,
posted on 2011/11/11 07:39:53 AM CET
It would be very helpful to develop a Library for MRF24WB0MA Wi-Fi transceiver, just like the one for ENC28J60 Ethernet controller.
I am looking forward for your reply.
Thank you !
Best regards,
Radu-Liviu PERETEANU
Military Technical Academy, Bucharest
posted on 2011/10/25 04:03:10 PM CEST
posted on 2011/10/19 08:02:39 AM CEST
hi i wana use a MMC card. i hve the impression that the buffer is overfloding.
could someone check the code and tell me what i did wrong....
Code:
void sd_card_init()
{
// Initialisiere SPI für MMC Zugriff
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 64, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
Delay_ms(10);
// Initialize MMC
if (!Mmc_Fat_Init())
{
// Falls MMC Init nicht i.O. ist SPI schneller initialisieren.
SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 4, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
}
}
void sd_card_read()
{
int zi = 0, zb = 0, zc = 0 ,zd = 0; // Schlaufenzähler
Mmc_Fat_Assign("config.txt", 0); //File config.txt öffnen
Mmc_Fat_Reset(&size); //grösse des Files auslesen und in size speichern
//File Auslesen und in buffer schreiben
for (zi = 1; zi <= size; zi++)
{
Mmc_Fat_Read(&buffer[0]);
//Wenn ein "-" (ASCII Code 45)
if (buffer[0] == 45)
{
zb = 0;
//Hier wird eine Zeile eingelesen (bis zum nächsten CR ASCII 13)
//oder 200 Zeichen eingelesen wurden
while (buffer [zb] != 13)
{
zb++;
Mmc_Fat_Read(&buffer[zb]);
if (zb == 200) break;
}
//Die ersten 4 Bytes des buffer werden in das Kommandobyte kopiert
zc = 0; //Schlaufenzähler auf 0 setzen
while (zc != 4) //Schlaufe um 4 bytes zu lesen
{
buffer_kdo[zc] = buffer[zc+1];
zc++;
}
//Byte 6 bis Byte 26 werden in buffer_ebene gespeichert
zd = 0; //Schlaufenzähler auf 0 setzen
while (zd != 20) //Schlaufe um 20 bytes zu lesen
{
buffer_ebene[zd] = buffer[zd+6];
zd++;
}
fill_vars();
}
}
}
void fill_vars()
{
//In dieser Funktion wird ein gefülltes Kommandobyte und Ebenenbyte
//vom Puffer in die entsprechende Variable ebenen_bez abgefüllt
//
//Funktionsinterne Variablendeklaration
int ebene_einer = 0 , ebene_zehner=0, ebene_total =0; //Ebenen Vraiablen
int ia = 0, i=0; //Schlaufen Variablen
//oder Ebenen Text
if (buffer_kdo[0] != 'E') return;
if (buffer_kdo[1] == 'B')
{
ebene_einer = buffer_kdo[3]-48;
ebene_zehner = buffer_kdo[2]-48;
ebene_total = (ebene_zehner * 10) + ebene_einer;
//4 Bytes Ebenentext in entsprechendes Array abfüllen
while (ia != 5)
{
ebenen_bez [ebene_total] [ia] = buffer[ia+6];
ia++;
}
}
//Ebenen Text in Array kopieren
ia = 0;
if (buffer_kdo[1] == 'T')
{
ebene_einer = buffer_kdo[3]-48;
ebene_zehner = buffer_kdo[2]-48;
ebene_total = (ebene_zehner * 10) + ebene_einer;
//Bytes Ebenentext in entsprechendes Array abfüllen
while (ia != 28)
{
ebenen_text [ebene_total] [ia] = buffer[ia+6];
ia++;
}
}
for (i = 0; i <= 128; i++)
{
buffer[i] = 0;
}
}
thanks