USB serial input from PIC16F690? cant get the tx pin to output and into mac

Discussion in 'Mac Programming' started by fabien, Oct 24, 2010.

  1. macrumors newbie

    Joined:
    Oct 24, 2010
    #1
    Hi!

    I cant seem to get any sensor data from the analog pins of a PIC16F690 into my mac via a serial-to-usb converter. Is there any problems in my code below, anyone? Not sure if have set up the TXREG to out to the tx pin correctly..... please help! Im going crazy!! fabien


    Code:
    #include <pic.h>		// Include HITECH CC header file
    
    __CONFIG (INTIO & WDTDIS & PWRTDIS & MCLRDIS & BORDIS & UNPROTECT & IESODIS & FCMDIS ); 
    
    unsigned short delay=50;	// Initialize on/off delay value to 50 msec
    int advalue = 0;			// Create A/D storage value and clear it
    
    // FUNCTION PROTOTYPES
    
    void pause( unsigned short msvalue );	// Establish pause routine function before main function
    void msecbase( void );					// Establish millisecond base function before main function
    void sendserial_twobyte (unsigned char control_number, unsigned char control_data);
    
    // MAIN FUNCTION
    
    
    main()
    {
    TRISA =	0b00010111;	// RA0, RA1, RA2, RA4 = ins, others out
    TRISB = 0b11111111;	// all PortC I/O set as outs
    TRISC = 0b00001111;	// RC0, RC1, RC2, RC3 ins, others outs
    ANSEL = 0b11111111;	// Select A/D inputs AN0, AN1, AN2, AN3, AN4, AN5, AN6, AN7
    ANSELH =0b00000000; 
    ADCON0 =0b00000000;	// left justified					
    ADCON1 =0b01010000;	// set ADC clock source and freq
    ADON = 1; // Turn on the AD unit
    CM1CON0 = 0;	//  Comparator 1 off
    CM2CON0 = 0;	//  Coparator 2 off
    
    // ANALOG CHANNEL 1 (AN0)
    while(1==1)	
    	{
    	CHS3 = 0; CHS2 = 0; CHS1 = 0; CHS0 = 1; // set channel AN0	
    	pause(delay);  	// delay function		
    	GODONE = 1;	// start the A-to-D process 	
    	while(GODONE) continue; 
    	advalue = ADRESH;// A-to-D result to "advalue"
    	sendserial_twobyte (240, advalue);  // send  two bytes
    }
    // ANALOG CHANNEL 2 (AN1)
    while(1==1)	
    	{
    	CHS3 = 0; CHS2 = 0; CHS1 = 1; CHS0 = 0;
    	pause(delay);	
    	GODONE = 1;	
    	while(GODONE) continue; 
    	advalue = ADRESH;
    	sendserial_twobyte (240, advalue);
    }
    //etc
    
    } // end main loop
    
    
    void pause( unsigned short msvalue )
    {
    	unsigned short x;					// declare a local variable "x" 	
    	for (x=0; x<=msvalue; x++)			//  Loop through a delay equal to msvalue
    		{								//  in milliseconds.
    		msecbase();						//  Jump to millisec delay routine
    		}								// end of for loop
    }										// end of pause function
    
    void msecbase(void)
    {										// start of msecbase function
    	OPTION = 0b00000001;				// set prescaler to TMRO 1:4
    	TMR0 = 0xd;							// preset TMRO to overflow on 250 counts
    	while(!T0IF);						// stay until TMRO overflow flag equals 1
    	T0IF = 0;							// clear the TMR0 overflow flag
    }										// end of msecbase function
    
    //*******************************************************
    //stransmits advalue and anlog number through to tx pin to USB serial on mac
    //*******************************************************
    void sendserial_twobyte (unsigned char control_number, unsigned char control_data) // no semicolon!
    {	
    	while(!TRMT) { }  		// hold if TXREG full (TRMT = 0 when TXREG full, = 1 when empty)
    	TXREG = control_number;	// send controller number
    	while(!TRMT) { } 		// hold again while TXREG full
    	TXREG = control_data;		// send controller data
    	
    	// this is the bit I cant get into the serial-to-USB converter into the mac...
    	
    } // end function
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Wirelessly posted (Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10)

    I always find an oscilloscope useful for debugging serial. Since most serial is in the audio range you can even use one of the iPhone apps that use the audio in. Stick a scope on the pin and see what you see if it matches your expectation. Or even just stick a speaker/headphones on the pin to see if you are getting a signal.

    B
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    Sounds like a job for... Das Blinkenlights! Mit eine smallen transistoren, und eine LED, und rezistorz.
    http://en.wikipedia.org/wiki/Blinkenlights

    Or a simple logic probe:
    http://en.wikipedia.org/wiki/Logic_probe
     
  4. kzg
    macrumors member

    Joined:
    Sep 18, 2004
    Location:
    Canada
    #4
    Hmm... I've only programmed the PIC in assembly, so I'm not sure how much I can help.

    But did you set up the baud rate generator (SPBRG and SPBRGH?) and I believe you have to set some bits in register TXSTA, such as your TXEN (Transmit Enable), SYNC (your probably want asynchronous) and your clock source.
     
  5. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    Yup, but I'm an analog guy. ;) (and it's hard to get a logic probe for <$10)

    The scope (or a decent logic analyzer) can potentially give you more info over blinkenlights to see if your baud rate is right. This happened to me on a PIC project where I had misunderstood the baud rate calculation as was putting out 2400 instead of 9600.

    B
     

Share This Page