Arya Kumar
Member level 4
Code:
#include "SPI.h"
//sbit Chip_Select at PORTB0_bit;
//sbit Chip_Select_Direction at DDB0_bit;
#define reset PORTB.f1
#define CS PORTB.f0
//-------------------------------------------------------------------------------------------------
// porta A
unsigned char const MCP2317_IODIRA = 0x00; // direction
unsigned char const MCP2317_IPOLA = 0x02; // polarity of gpio- setting one inverts
unsigned char const MCP2317_GPINTENA = 0x04; // interrupt
unsigned char const MCP2317_DEFVALA = 0x06; // default value change for Interrupt to occur
unsigned char const MCP2317_INTCONA = 0x08; //interrupt on changae
unsigned char const MCP2317_IOCONA = 0x0A; // configuration register
unsigned char const MCP2317_GPPUA = 0x0C; // pull up enable or disable
unsigned char const MCP2317_INTFA = 0x0E; // Interrupt flag register
unsigned char const MCP2317_INTCAPA = 0x10; ///interrupt capture register
unsigned char const MCP2317_GPIOA = 0x12; // PIN state register
unsigned char const MCP2317_OLATA = 0x14; // reading pin values
// porta B
unsigned char const MCP2317_IODIRB =0x01;
unsigned char const MCP2317_IPOLB =0x03;
unsigned char const MCP2317_GPINTENB =0x05;
unsigned char const MCP2317_DEFVALB =0x07;
unsigned char const MCP2317_INTCONB =0x09;
unsigned char const MCP2317_IOCONB =0x0B;
unsigned char const MCP2317_GPPUB =0x0D;
unsigned char const MCP2317_INTFB =0x0F;
unsigned char const MCP2317_INTCAPB =0x11;
unsigned char const MCP2317_GPIOB =0x13;
unsigned char const MCP2317_OLATB =0x15;
unsigned char const MCP2317_OPCODE_WRITE =0x40;
unsigned char const MCP2317_OPCODE_READ =0x41;
unsigned char letto_a=0x00 ; //absolute 0x0020; // forces the variable to be in a particular location
unsigned char letto_b=0x00 ; //absolute 0x0021;
void MCP2317_Write(unsigned char const opcode,unsigned char const address, unsigned char dato);
unsigned char MCP2317_Read(unsigned char const opcode, unsigned char const address);
//-------------------------------------------------------------------------------------------------
void main()
{
DDRB.B0=0;
DDRB.B1=0;
//SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
SPI1_Init();
PORTB.f1=0;
delay_ms(100);
PORTB.f1=1;
//--------------- configur IOCONA-------------------//
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_IOCONA,0x20); //seq operation disabled
//----------------configuro registro IOCONB------------------//
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_IOCONB,0x20); //seq operation disabled
//----------------configur porta B ------------//
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_IODIRB,0x00); // direction to output
//----------------scrivo sulla porta B 0x00-------------------//
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_OLATB,0x00); /// ?????
//----------------configur porta A -------------//
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_IODIRA,0xFF); //direction to input
while (1)
{
letto_b=MCP2317_Read( MCP2317_OPCODE_READ,MCP2317_GPIOA);
letto_b = letto_b & 0x01;
if(letto_b == 0x01)
{
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_GPIOB,0xF0); //MCP2317_OLATB both are used - isnt clear which one has to be used
Delay_ms(100);
//Lcd_Out(2,17,"yes we have it");
}
else
{
MCP2317_Write(MCP2317_OPCODE_WRITE,MCP2317_GPIOB,0x0F); //MCP2317_OLATB both are used - isnt clear which one has to be used
Delay_ms(100);
}
Delay_ms(100);
}
}
void MCP2317_Write( unsigned char const opcode,unsigned char const address,
unsigned char dato)
{
PORTB.f0=0;
SPI1_Write(opcode);
SPI1_Write(address);
SPI1_Write(dato);
PORTB.f0=1;
}
unsigned char MCP2317_Read(unsigned char const opcode,
unsigned char const address)
{
unsigned char letto_p;
PORTB.f0=0;
SPI1_Write(opcode);
SPI1_Write(address);
letto_p=SPI1_Read(0);
PORTB.f0=1;
return letto_p;
}
Almost same code 1.zip works but 2.zip doesnt please help