This shows you the differences between two versions of the page.
— |
project:usb:code:goodlink [2007/04/27 00:21] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== GoodLink(TM) code ====== | ||
+ | Below is the code I needed to get the PDIUSBD12' | ||
+ | |||
+ | Below is a screenshot of HyperTerminal interacting with the system running this code. For more information on the PIC <--> PC setup, see [[: | ||
+ | |||
+ | {{ project: | ||
+ | |||
+ | This code assumes you are using either a dumb-terminal or equivalent (I was using HyperTerminal) to view the results of '' | ||
+ | |||
+ | This code was made for MPLAB v7.0, with the CCS add-in. | ||
+ | |||
+ | * {{project: | ||
+ | * {{project: | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // | ||
+ | // Setup PIC and CCS compiler | ||
+ | #fuses XT, PUT, NOWDT, NOPROTECT | ||
+ | #use delay(clock = 4000000) | ||
+ | |||
+ | #use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7) | ||
+ | // By using C6 and C7, we will make use of the 877's hardware USART abilities | ||
+ | |||
+ | #use standard_io(b) | ||
+ | #byte port_d = 8 | ||
+ | |||
+ | // | ||
+ | // Definitions | ||
+ | #define ON 1 | ||
+ | #define OFF 0 | ||
+ | |||
+ | #define D12_A0 PIN_B6 | ||
+ | #define D12_WR_N PIN_B2 | ||
+ | #define D12_RD_N PIN_B1 | ||
+ | #define D12_RESET_N PIN_B5 | ||
+ | #define D12_SUSPEND PIN_B4 | ||
+ | #define D12_INT_N PIN_B0 | ||
+ | |||
+ | #define D12_DATA 0 | ||
+ | #define D12_COMMAND 1 | ||
+ | |||
+ | #define LED_N PIN_B7 | ||
+ | |||
+ | // | ||
+ | // Global Variable Declarations | ||
+ | |||
+ | |||
+ | // | ||
+ | // Function prototypes | ||
+ | void D12Write(short, | ||
+ | void D12Read(unsigned char, int); | ||
+ | void D12InterruptHandler(); | ||
+ | |||
+ | // | ||
+ | // Entry point | ||
+ | void main(void){ | ||
+ | short debug = OFF; | ||
+ | |||
+ | output_low(LED_N); | ||
+ | |||
+ | while(debug) | ||
+ | { | ||
+ | printf(" | ||
+ | delay_ms(1000); | ||
+ | output_toggle(LED_N); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | set_tris_b(0x01); | ||
+ | set_tris_d(0x00); | ||
+ | port_d = 0xFF; | ||
+ | |||
+ | output_high(D12_RD_N); | ||
+ | output_high(D12_WR_N); | ||
+ | |||
+ | output_low(D12_RESET_N); | ||
+ | output_low(D12_A0); | ||
+ | output_low(D12_SUSPEND); | ||
+ | |||
+ | disable_interrupts(GLOBAL); | ||
+ | |||
+ | ext_int_edge(H_TO_L); | ||
+ | enable_interrupts(INT_EXT); | ||
+ | clear_interrupt(INT_EXT); | ||
+ | enable_interrupts(GLOBAL); | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | |||
+ | // | ||
+ | |||
+ | getch(); | ||
+ | output_high(D12_RESET_N); | ||
+ | delay_ms(1); | ||
+ | |||
+ | // | ||
+ | // | ||
+ | |||
+ | printf(" | ||
+ | D12Write(D12_COMMAND, | ||
+ | |||
+ | printf(" | ||
+ | D12Write(D12_DATA, | ||
+ | |||
+ | printf(" | ||
+ | D12Write(D12_COMMAND, | ||
+ | printf(" | ||
+ | D12Write(D12_DATA, | ||
+ | |||
+ | printf(" | ||
+ | D12Write(D12_COMMAND, | ||
+ | printf(" | ||
+ | printf(" | ||
+ | D12Write(D12_DATA, | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | D12Write(D12_DATA, | ||
+ | |||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // Used for passing commands or data to the PDIUSBD12 | ||
+ | void D12Write(short type, int data) | ||
+ | { | ||
+ | switch(type) | ||
+ | { | ||
+ | case D12_DATA: | ||
+ | case D12_COMMAND: | ||
+ | set_tris_d(0x00); | ||
+ | output_high(D12_RD_N); | ||
+ | |||
+ | if(type == D12_COMMAND) | ||
+ | output_high(D12_A0); | ||
+ | else | ||
+ | output_low(D12_A0); | ||
+ | |||
+ | port_d = data; // Setup bus | ||
+ | delay_ms(1); | ||
+ | output_low(D12_WR_N); | ||
+ | output_high(D12_WR_N); | ||
+ | |||
+ | if(type == D12_COMMAND) | ||
+ | output_low(D12_A0); | ||
+ | break; | ||
+ | default: | ||
+ | printf(" | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // Used for reading data from the PDIUSBD12 | ||
+ | void D12Read(unsigned char* buffer, int reads) | ||
+ | { | ||
+ | int i; | ||
+ | |||
+ | set_tris_d(0xFF); | ||
+ | for(i = 0; i<reads; i++) | ||
+ | { | ||
+ | output_low(D12_RD_N); | ||
+ | buffer[i] = port_d; // Latch in the bus | ||
+ | output_high(D12_RD_N); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // FIXME: Probably want to save some registers when handling | ||
+ | // this interrupt, as it takes quite a long time. | ||
+ | #INT_EXT | ||
+ | void D12InterruptHandler() | ||
+ | { | ||
+ | unsigned char buffer[2]; | ||
+ | int i; | ||
+ | |||
+ | // The first " | ||
+ | // [1;31m makes text red and bold, [0;37m makes it normal and white | ||
+ | printf(" | ||
+ | printf(" | ||
+ | D12Write(D12_COMMAND, | ||
+ | D12Read(buffer, | ||
+ | printf(" | ||
+ | getch(); | ||
+ | |||
+ | for(i=0; i<6; i++) | ||
+ | { | ||
+ | printf(" | ||
+ | |||
+ | D12Write(D12_COMMAND, | ||
+ | D12Read(buffer, | ||
+ | |||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | getch(); | ||
+ | } | ||
+ | </ | ||