» »

[koda] PIC kot webserver

[koda] PIC kot webserver

Azrael ::

Začasna rešitev, ker programska koda raztegne stran in onemogoči enostavno branje izvorne teme.:
ql000

registriran:
20.12.2003 Poslano: 29.01.2006 09:40:08

IIM7010A se da dobiti v svetu lektronike za 10k SIT. Sej vem, da je drag kot žafran, samo je pa edina rešitev v svetu, ki ima protokole hardware-sko rešene. Z modulom se da komunicirat preko I2C ali pa ga uporabljaš kot memorijo (paralelna komunikacija), samo slednja zahteva ful več pinov na uC-ju, je pa ful ful hitrejša. Pač stvar izbire.Proizvajalec tega modula (Wiznet; klik) nudi kodo samo za 8051 arhitekturo, tako, da če hočeš implementirat zadevo na PIC-u je malo telovadbe, vendar samo pri spreminjanju kode za I2C in pa a interupte, vse ostalo se da ponucat! Če kdo rabi kodo od proizvajalca naj mi pove, ker je ni možno zloadat z njihove strani. Jaz sem jih žical na forumu, da so mi jo dali!

Evo še kode. Za kompajlanje potrebujete MPLab in C18. C18 student edition je zastonj, pravtako MPLab (Microchip). I2C adresa modula se določi hardwaresko s stanjem na pinih A14-A8, pri čemer morate biti pozorni na zadnji bit, saj ta definira Write (0) / Read (1) condition. Torej, če je slave adresa modula 0x7F + 0 zadnji bit (write) = 0xFE.Ostalo se vidi iz električne sheme.

Aja, pa koda je še malo špagetasta, ker sem se malo igral s parametri, sam v glavnem dela! Bom pa po izpitih napisal novo kodo, sedaj vsaj vem kako!


#include <p18f452.h>
#include <i2c.h>
#include "pic_socket.h"
#include <usart.h>
#include <stdio.h>
#include <portb.h>
#include <delays.h>
#include <stdlib.h>

void rx_handler (void);
unsigned char WriteB(unsigned char reg_addr_high,unsigned char reg_addr_low, unsigned char data);
unsigned char WriteBWA(unsigned char reg_addr_high, unsigned char reg_addr_low, unsigned char data);
void setup_USART(void);
void set_fastIPs(void);
void set_slowIPs(void);
void set_fastIPsGeneral(void);

#pragma config OSC = HS
#pragma config OSCS = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF
#pragma config STVR = OFF
#pragma config CCP2MUX = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF


#pragma code Interrupt_High = 0x0008
void rx_int (void)
{
  _asm goto rx_handler _endasm
}
#pragma code


#pragma interrupt rx_handler
void rx_handler (void)
{
  unsigned char select;
  unsigned char sl_address;
  sl_address = 0xFE;

  /* Get the character received from the USART */
  select = ReadUSART();

		switch(select)
		{
			case 'a':
				putrsUSART((const far rom char *)" \n\r Prenasam podatke W3100A... \n\r");
				putrsUSART((const far rom char *)" \n\r Nastavljam IP stevilke... \n\r");

				set_slowIPs();

			break;

			case 'b':
				putrsUSART((const far rom char *)" \n\r Prenasam podatke W3100A... \n\r");
				putrsUSART((const far rom char *)" \n\r Nastavljam IP stevilke... \n\r");

				set_fastIPs();
			break;

			case 'c':
				printf(" \n\n\r Test generirane i2c komunikacije: \n\r");
				printf(" \r Test posiljanja adrese BREZ ACK bit... \n\r");
				printf(" \n Test zapisa adrese slava: %ud\n\r", sl_address);
				
				OpenI2C(MASTER, SLEW_ON);
				SSPADD = 11;				
				Delay1KTCYx(30);
				IdleI2C();
				StartI2C();
				IdleI2C();																															// Inicializacija I2C
				WriteI2C(0xFE);					// Slave device address
				IdleI2C();
				StopI2C();
				IdleI2C();
				CloseI2C();
			break;

			case 'd':
				printf("\n\n\r Test I2C komunikacije, 400kHz,...\n\r");
				printf("\n\n\r Uporaba General Call address...\n\r");

				set_fastIPsGeneral();
			break;
		}
	
    PIR1bits.RCIF = 0;
} 

unsigned char WriteB(unsigned char reg_addr_high, unsigned char reg_addr_low, unsigned char data)
{
	Delay10KTCYx(10);

	IdleI2C();
	StartI2C();

	IdleI2C();																								
	WriteI2C(0xFE);	// Slave address
	//AckI2C();
	IdleI2C();

	/*if(SSPCON2bits.ACKSTAT == 0)
	{
   		printf("\n\r Ack Recieved after writing slave address for Lower byte while reading");
	}
	else
	{
   		printf("\n\r No ack Recieved after writing slave address");
	}*/
																																								
	WriteI2C(reg_addr_high);
	//AckI2C();
	IdleI2C();

	WriteI2C(reg_addr_low);
	//AckI2C();
	IdleI2C();

	WriteI2C(data);
	//AckI2C();
	IdleI2C();

	StopI2C();
	IdleI2C();
}


unsigned char WriteBWA(unsigned char reg_addr_high, unsigned char reg_addr_low, unsigned char data)
{
	Delay10KTCYx(50);

	IdleI2C();
	StartI2C();

	IdleI2C();																								
	WriteI2C(0xFE);	// General call to all slaves
	AckI2C();
	IdleI2C();

	if(SSPCON2bits.ACKSTAT == 0)
	{
   		printf("\n\r Ack Recieved after writing slave address for Lower byte while reading");
	}
	else
	{
   		printf("\n\r No ack Recieved after writing slave address");
	}
																																								
	WriteI2C(reg_addr_high);
	AckI2C();
	IdleI2C();

	WriteI2C(reg_addr_low);
	AckI2C();
	IdleI2C();

	WriteI2C(data);
	AckI2C();
	IdleI2C();

	StopI2C();
	IdleI2C();
}

void set_fastIPs(void)
{
	OpenI2C(MASTER, SLEW_OFF);
	SSPADD = 49;				//[SSPAD=Fosc/(4*clock)-1]->clock 100kHz = SSPADD 49

	WriteB(0x00, 0x00, 0x80);		// W3100A restart

	// Gateway IP setup
	WriteB(0x00, 0x80, 0);
	WriteB(0x00, 0x81, 0);
	WriteB(0x00, 0x82, 0);
	WriteB(0x00, 0x83, 0);
	
	// Subnet mask IP setup
	WriteB(0x00, 0x84, 255);
	WriteB(0x00, 0x85, 255);
	WriteB(0x00, 0x86, 255);
	WriteB(0x00, 0x87, 0);

	// MAC address setup
	WriteB(0x00, 0x88, 0x00);
	WriteB(0x00, 0x89, 0x00);
	WriteB(0x00, 0x8A, 0x00);
	WriteB(0x00, 0x8B, 0x00);
	WriteB(0x00, 0x8C, 0x00);
	WriteB(0x00, 0x8D, 0x00);

	// User IP setup
	WriteB(0x00, 0x8E, 192);
	WriteB(0x00, 0x8F, 168);
	WriteB(0x00, 0x90, 0);
	WriteB(0x00, 0x91, 8);

	WriteB(0x00, 0x00, 0x01);		// Initialize W3100A
	
	CloseI2C();
	
}

void set_slowIPs(void)
{
	OpenI2C(MASTER, SLEW_OFF);
	SSPADD = 49;				//[SSPAD=Fosc/(4*clock)-1]->clock 100kHz = SSPADD 49

	WriteB(0x00, 0x00, 0x80);		// W3100A restart

	// Gateway IP setup
	WriteB(0x00, 0x80, 0);
	WriteB(0x00, 0x81, 0);
	WriteB(0x00, 0x82, 0);
	WriteB(0x00, 0x83, 0);
	
	// Subnet mask IP setup
	WriteB(0x00, 0x84, 255);
	WriteB(0x00, 0x85, 255);
	WriteB(0x00, 0x86, 255);
	WriteB(0x00, 0x87, 0);

	// MAC address setup
	WriteB(0x00, 0x88, 0x00);
	WriteB(0x00, 0x89, 0x00);
	WriteB(0x00, 0x8A, 0x00);
	WriteB(0x00, 0x8B, 0x00);
	WriteB(0x00, 0x8C, 0x00);
	WriteB(0x00, 0x8D, 0x00);

	// User IP setup
	WriteB(0x00, 0x8E, 192);
	WriteB(0x00, 0x8F, 168);
	WriteB(0x00, 0x90, 0);
	WriteB(0x00, 0x91, 8);

	WriteB(0x00, 0x00, 0x01);		// Initialize W3100A
	
	CloseI2C();
	
}

/* Test general call adrese*/
void set_fastIPsGeneral(void)
{
	OpenI2C(MASTER, SLEW_OFF);
	SSPADD = 49;				//[SSPAD=Fosc/(4*clock)-1]->clock 100kHz = SSPADD 49

	WriteBWA(0x00, 0x00, 0x80);		// W3100A restart

	// Gateway IP setup
	WriteBWA(0x00, 0x80, 0);
	WriteBWA(0x00, 0x81, 0);
	WriteBWA(0x00, 0x82, 0);
	WriteBWA(0x00, 0x83, 0);
	
	// Subnet mask IP setup
	WriteBWA(0x00, 0x84, 255);
	WriteBWA(0x00, 0x85, 255);
	WriteBWA(0x00, 0x86, 255);
	WriteBWA(0x00, 0x87, 0);

	// MAC address setup
	WriteBWA(0x00, 0x88, 0x00);
	WriteBWA(0x00, 0x89, 0x00);
	WriteBWA(0x00, 0x8A, 0x00);
	WriteBWA(0x00, 0x8B, 0x00);
	WriteBWA(0x00, 0x8C, 0x00);
	WriteBWA(0x00, 0x8D, 0x00);

	// User IP setup
	WriteBWA(0x00, 0x8E, 192);
	WriteBWA(0x00, 0x8F, 168);
	WriteBWA(0x00, 0x90, 0);
	WriteBWA(0x00, 0x91, 8);

	WriteBWA(0x00, 0x00, 0x01);		// Initialize W3100A
	
	CloseI2C();
	
}

void setup_USART(void)
{
	OpenUSART
	(
		USART_TX_INT_OFF  &
		USART_RX_INT_ON   &
		USART_ASYNCH_MODE &
		USART_EIGHT_BIT   &
		USART_CONT_RX	  &
		USART_BRGH_HIGH,
		65
	);

	/* Enable interrupt priority */
	RCONbits.IPEN = 1;
	/* Make receive interrupt high priority */
	IPR1bits.RCIP = 1;
	/* Enable all high priority interrupts */
	INTCONbits.GIEH = 1;
}

void main(void)
{
	TRISB = 0;

	TRISA = 0;
	PORTA = 0;
	
	PORTA = 0x2;

	DDRCbits.RC3 = 1;
	DDRCbits.RC4 = 1;

	setup_USART();

  /* Display a prompt to the USART */
  putrsUSART ((const far rom char *)"\n\r Vnesi izbiro menija... \n\r");

  /* Loop forever */
  while (1);
	
}                                                                                                                                                         
Nekoč je bil Slo-tech.
  • spremenil: Azrael ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[C++] problem z binarnimi datotekami

Oddelek: Programiranje
9944 (809) mallard
»

NEC ND-3540A problem...

Oddelek: Pomoč in nasveti
282387 (1678) dean444
»

LG GSA-4120B dela probleme pri pečenju DL DVD+Rjev

Oddelek: Strojna oprema
441703 (1512) mtosev
»

Težave s pekačem

Oddelek: Pomoč in nasveti
121870 (1810) boštjan
»

PLEXTOR BurfProof unicuje cdje - prosim pomagajte!

Oddelek: Pomoč in nasveti
91630 (1384) krho

Več podobnih tem