00001
00002
00003
00004
00005 #include "piccolo_all.h"
00006 #include "piccolo_spi.h"
00007 #include "delays.h"
00008
00020 #define MCP23S17ADDR 0x40 //MCP23S17 címe, földre kötött címvonalakkal
00021 #define IODIRA_CIME 0x00
00022 #define IODIRB_CIME 0x01
00023 #define OLATA_CIME 0x14
00024 #define OLATB_CIME 0x15
00025
00026 char cmd[32];
00027 uint8 count,regaddr,data;
00028
00029 uint8 chr2hex(char a, char b) {
00030 if(a>0x40) {a -=7;}
00031 if(b>0x40) {b -=7;}
00032 return(((a & 0x0F)<<4) + (b & 0x0F));
00033 }
00034
00035
00036 int main (void) {
00037 InitializeSystem();
00038 DISABLE_ALL_ANALOG();
00039 mInitAllLEDs();
00040 LEDport = 0;
00041 SLAVE_DISABLE();
00042 while (!usb_cdc_kbhit()) {
00043 ProcessIO();
00044 }
00045 outString("\nIsten hozott a PICCOLO projekthez!\n");
00046 outString("mcp23017_test.c program, MCU: ");
00047 #if defined(__18F4550)
00048 outString("PIC18F4550\n");
00049 #elif defined(__18F14K50)
00050 outString("PIC18F14K50\n");
00051 #endif
00052
00053
00054 PR2 = 59;
00055 OpenTimer2(TIMER_INT_OFF | T2_PS_1_1 | T2_POST_1_11 );
00056
00057 spi_init(CKP_ACTIVE_HIGH |
00058 MASTER_TMR2,
00059 SPI_CKE_ON |
00060 SPI_SMP_END);
00061 MCP23S17_write(IODIRA_CIME,0);
00062 MCP23S17_write(IODIRB_CIME,0);
00063 MCP23S17_write(OLATA_CIME,0);
00064 MCP23S17_write(OLATB_CIME,0);
00065 while (1) {
00066 outString("command: ");
00067 count=inStringEcho(cmd,30);
00068 if(cmd[0]=='#' && count>3) {
00069 regaddr=chr2hex(cmd[2],cmd[3]);
00070 switch(cmd[1]) {
00071 case 'W':
00072 data=chr2hex(cmd[4],cmd[5]);
00073 MCP23S17_write(regaddr,data);
00074 break;
00075 case 'R':
00076 MCP23S17_read(regaddr,&data);
00077 printf("Reg%d = %X\n",regaddr,data);
00078 break;
00079 default:
00080 outString("Pardon?\n");
00081 break;
00082 }
00083 }
00084 else {
00085 outString("Valid commands are:");
00086 outString("#Wrrdd - write dd to register rr");
00087 outString("#Rrr - read register rr");
00088 outString("#Srr - set speed to rr x 100 kHz ");
00089 }
00090 doHeartbeat();
00091 }
00092 }